saving all for loop outputs

1 回表示 (過去 30 日間)
amna chaudhary
amna chaudhary 2018 年 4 月 15 日
編集済み: Geoff Hayes 2018 年 4 月 15 日
I have the following code for which I want to store all the outputs in an excel file. However when i try to do so i only get the last output of for loop. PLEASE HELP
seq = [];
seq2 = [];
aa = ['A', 'R', 'D'];
bb = perms(aa);
c = cellstr(bb);
d = char(c);
for j = 1:length(d)
if d(1,1) == 'A'
seq = [seq, A(1)];
elseif d(1,1) == 'R'
seq = [seq, R(1)];
elseif d(1,1) == 'D'
seq = [seq, D(1)];
end
if d(1,2) == 'A'
seq = [seq, A(2)];
elseif d(1,2) == 'R'
seq = [seq, R(2)];
elseif d(1,2) == 'D'
seq = [seq, D(2)];
end
if d(1,3) == 'A'
seq = [seq, A(3)];
elseif d(3) == 'R'
seq = [seq, R(3)];
elseif d(1,3) == 'D'
seq = [seq, D(3)];
seq = [];
end
seq;
seq2 = prod(seq)
end
  1 件のコメント
Rik
Rik 2018 年 4 月 15 日
You forgot to index your output. Your loop variable is used nowhere in your loop, so it repeats the same calculation each and every time.
What is it you want to do? Which variable do you want to index? A small example of how to create a loop that saves its output for every iteration is the code below.
a=zeros(10,1);%pre-allocate a vector to store results
for n=1:numel(a)
data=rand(1,4);%generate random data
data=sum(data);%do something with that data
a(n)=data;%store it in the vector
end

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

回答 (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