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

2 views (last 30 days)
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?

Accepted Answer

Guillaume
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
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
Stephen23
Stephen23 on 21 Dec 2018
Edited: Stephen23 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
Guillaume on 21 Dec 2018
Edited: 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)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!