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

Accepted Answer

Jan
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
Ross Johnston
Ross Johnston on 6 Mar 2017
Hi Jan, Thank you for the reply, I have simplified the code down a bit to do it in steps to try to make it a bit easier for me and tried to implement your code as follows:
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);
ProductprobCO = prod(probsCO);
n = 100;
FinalprobCO = zeros(1,n);
for loop = 1:n
Data = ProductprobCO
FinalprobCO(loop) = Data;
end
This gives me an array of 100 values but the are all of the same value, it is obvious to me that the loop is just reading the ProductprobCO 100 times and putting it into the array FinalprobCO. How do I implement the loop so that the whole chunk of code:
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);
ProductprobCO = prod(probsCO);
is incorporated into the loop to give different values for ProductprobCO each time and store these values into the array FinalprobCO ?
Ross Johnston
Ross Johnston on 6 Mar 2017
I think i have it sorted by doing this:
n = 100;
FinalprobCO = zeros(1,n);
for loop = 1:n
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);
ProductprobCO = prod(probsCO);
Data = ProductprobCO
FinalprobCO(loop) = Data;
end
Thanks again for the help!

Sign in to comment.

More Answers (0)

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!