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 (최근 30일)
이전 댓글 표시
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
채택된 답변
Jan
2017년 3월 6일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!