How to create a loop for a function 100 times and put the results into an array? Is for loop best and how to implement it?
3 views (last 30 days)
Show older comments
Hi there,
I have used the following code to produce a value for the FinalprobCO and the FinalprobAF.
%%compare the 5 control sampled values against the QTc values in QTcProb to obtain
%%the probability for each of the samples.
%%take 5 samples from the unsampled data from controls
COQTcsample1 = datasample (missValueCO, 5, 'Replace', false);
diffs1 = bsxfun( @minus, COQTcProb(1,:), COQTcsample1' );
[d, idx1] = min( abs( diffs1 ), [], 2 );
probsCO = COQTcProb(2,idx1);
FinTab1= vertcat(COQTcsample1, probsCO);
FinalprobCO = prod(probsCO);
%%compare the 5 AF sampled values against the QTc values in QTcProb to obtain
%%the probability for each of the samples.
AFQTcsample1 = datasample (missValueAF, 5, 'Replace', false);
diffs2 = bsxfun( @minus, COQTcProb(1,:), AFQTcsample1' );
[d, idx2] = min( abs( diffs2 ), [], 2 );
probsAF = COQTcProb(2,idx2);
FinTab2= vertcat(AFQTcsample1, probsAF);
FinalprobAF = prod(probsAF);
I would like to carry out the process 100 times to give 100 values for each and save them into two seperate arrays.
0 Comments
Accepted Answer
Jan
on 6 Mar 2017
The standard method is:
n = 100;
Result = zeros(1, n);
for k = 1:n
Data = ... your calculations
Result(k) = Data;
end
If the computed value is a vector of length m, use:
Result = zeros(m, n);
...
Result(:, k) = Data;
or if the different results have different sizes, use a cell array:
Result = cell(1, n);
...
Result{k} = Data;
2 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements 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!