MATLAB Answers

I need to pick random elements from array until a specific sum is met

9 views (last 30 days)
nikolaj hansen
nikolaj hansen on 21 Dec 2018
Edited: Guillaume on 21 Dec 2018
I have a 131x3 table with both text and numbers. I need to randomize the numbers until a specific sum is found and then print the corresponding text (from the same row as the found numbers).
my code so far:
C = table2cell(a);
N=10; % no. of rows needed
pick=randperm(length(C),N); % picks random numbers from the table
vector=cell2mat(C(:,3)); % all the 131 numbers from the vector
B=C(c,:); % outputs the text from the random numbers
array=cell2mat(B(:,3)); % saves the 10 random numbers in array
summen=sum(vec); % sum of the 10 random numbers which needs to be a specific number
disp(out); % prints the text corresponding to the random numbers picked
In this code I have to manually run the script until the sum displayed is a desired number. How do I automate this an do it more efficient?


Sign in to comment.

Accepted Answer

Guillaume on 21 Dec 2018
Use a while loop:
a = readtable('dom.txt'); %use a better variable name than a. One that has meaning.
summen = NaN; %initialise the result
while summen ~= targetsum
%your code to calculate the sum
You need to learn to manipulate tables. Your conversion from table to cell is a complete waste of time. You also need to learn to index cell arrays. Even if you were using a cell array, none of the cell2mat are necessary.
domtable = readtable('dom.txt'); %no conversion to cell array
pick = randperm(height(domtable), N); %get the height of the table instead of the length of cell array. Certainly don't use length
vector = domtable.(3); %or better use the variable name:
%vector = domtable.NameOf3rdColummn
chosenrows = domtable(pick, :);
summen = sum(domtable{pick, 3});
Also, never use length on a 2D array.


nikolaj hansen
nikolaj hansen on 21 Dec 2018
Thank you soo much!!
I don't know why I struggled so much with a simple while loop, kind of embarrassing (I tried to use while 1 with break).
Also thank you for the inputs to improve my code. The only Matlab I know is self taught and I don't have to present the code to anyone. But I really want to improve!
For a long time I have struggled with formatting tables. I use tables for string and cell2mat if I need to manipulate the numbers, else I recieve an error. But I guess I just stuck with a stupid solution because it once worked. I don't get why I shouldn't use length() though? I get that it is kind of weird using length() on a table.
but again.. thanks a lot!
Stephen Cobeldick
Stephen Cobeldick on 21 Dec 2018
You should not use length because the dimension that it measures along changes depending on the size of the input array, and this happens without any warning. Use size or numel rather than length.
Guillaume on 21 Dec 2018
Forget that length ever existed In my opinion, that function should be removed from matlab, it's such a source of bugs as it's often misused.
For tables, use height, width, or size with the proper dimension input.
For vectors, use numel
For anything else, use size with the proper dimension input.
As Stephen explains in his link, the dimension that length return is not fixed. In the code you have written, there is an implicit assumption that it returns the number of rows. The problem is that this implicit assumption is never checked. If for some reason, the table ends up having more columns than row, then length will be the number of columns instead and your code breaks. It may be that the assumption may never get broken at the moment, as the table is always 131x3 but maybe in the future the code is adapted to work on other size tables and one day somebody supply a 131x132 table and kaboom!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!