saving all for loop outputs

1 次查看(过去 30 天)
amna chaudhary
amna chaudhary 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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by