I need to pick random elements from array until a specific sum is met
Show older comments
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
More Answers (0)
Categories
Find more on Logical 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!