# how to save output of for loop

6 visualizzazioni (ultimi 30 giorni)
Safi ullah il 28 Feb 2017
Risposto: MARCO FORTI il 25 Ott 2019
Hi, I have a matrix A=54×100. For some specific condition I perform an operation on each row of “A” with the help of for loop. now I need to save the output of this for loop. I tried like
S=zeros(54,100);
for i=1:54;
Ri=A(i,:);
end
but not succeed.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Alexandra Harkai il 28 Feb 2017
The size of output S in this case will be 54*20. To specify that you want to assign answer to the whole row of S, use the same (i,:) indexing you used for A:
S=zeros(54,20);
for i=1:54;
Ri=A(i,:);
end
##### 2 CommentiMostra NessunoNascondi Nessuno
Safi ullah il 28 Feb 2017
@ Alexandra Harkai thanks,now the code work well.
Jan il 28 Feb 2017
+1, correct.

Accedi per commentare.

### Più risposte (2)

Jan il 28 Feb 2017
This works without a loop also:
S = squeeze(mean(reshape(Ri, size(A, 1), 5, 20), 2));
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Alexandra Harkai il 28 Feb 2017
+1 for teaching me squeeze

Accedi per commentare.

MARCO FORTI il 25 Ott 2019
Hello all,
I am doing forecasting and the algorithm works fine, nevertheless I can not save the output as I would.
In particular, the final loop is:
%final forecast
ff=zeros(horiz,1);
for i=1:horiz
xf=xb(1:end-i+1,:);
yf=yb(i:end);
betah=(xf'*xf)\(xf'*yf);
ff(i)=y(end-pbest+1:end)'*betah;
th=[th;ff(i)]; %
end
In this I only get the general output as "ans", while I would get the two vector "ff" and "th" both in sequence (as "ans" reports) and separately.
Can anyone help me?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by