saving all for loop outputs

1 Ansicht (letzte 30 Tage)
amna chaudhary
amna chaudhary am 15 Apr. 2018
Bearbeitet: Geoff Hayes am 15 Apr. 2018
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 Kommentar
Rik
Rik am 15 Apr. 2018
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

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by