MATLAB Answers

0

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

Asked by nikolaj hansen on 21 Dec 2018
Latest activity Edited by Guillaume
on 21 Dec 2018
Hi
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:
a=readtable('dom.txt');
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
out=B(:,1);
disp(out); % prints the text corresponding to the random numbers picked
disp(summen);
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?

  0 Comments

Sign in to comment.

1 Answer

Answer by Guillaume
on 21 Dec 2018
 Accepted Answer

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
end
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.

  3 Comments

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!
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.
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.