saving all for loop outputs
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Rik
el 15 de Abr. de 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
Respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!