Loop to generate random numbers and compile results for each iterations

4 ビュー (過去 30 日間)
JL
JL 2019 年 8 月 27 日
コメント済み: JL 2019 年 8 月 28 日
Hi everyone, I've been trying to do a loop (i know I could just use rand(100000000,5) for what I want to achieve. May I know if there's any way I can compile results from the first iterations to the last??
for iter = 1:100 %run rand 1000 times
a=rand(1000000,5);
b=[0.02 0.03 0.11 0.02 0.05];
res = bsxfun(@gt,a,b)
d = bi2de(res);
dsubset=size(d,1);
dones=ones(dsubset,1);
[G,ID] = findgroups(d);
D = [splitapply(@sum,dones,G),ID];
C = [splitapply(@sum,dones,G),ID];
C(:,1)=[];
bC=de2bi(C);
Compile = [D,bC] %this be updated for new unique rows generated plus how many times they appear
end

採用された回答

Stephen23
Stephen23 2019 年 8 月 28 日
編集済み: Stephen23 2019 年 8 月 28 日
You could easily use a cell array:
N = 100;
Z = cell(1,N);
for k = 1:N
... your code
Z{k} = whatever you want to store
end
The stored data from each iteration is available in Z.
After the loop you can likely concatenate the contents of the cell array using
A = cat(1,Z{:}) % Or 2, depending on the array sizes.
  3 件のコメント
Stephen23
Stephen23 2019 年 8 月 28 日
"What is k?"
I copied part of your code and forgot to change the loop iteration variable. I have corrected my answer.
JL
JL 2019 年 8 月 28 日
Thanks Stephen. Very helpful indeed.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by