Azzera filtri
Azzera filtri

How do I Loop a code every 6 rows until the end?

1 visualizzazione (ultimi 30 giorni)
I need to calculate daily n-factors for a complete year. I use this code;
trapz(x(1:6,1),min(y(1:6,3),0)/trapz(x(1:6,1),min(y(1:6,2),0)
1:6 is 1st day, 7:12 is 2nd and so on... I have 1800 measurements and I need to perform this code every 6 rows from matrix Y so I have another matrix corresponding to the DAILY calculations of the n-factor.
How do I loop this code every 6 rows?
-
Regards!
Sebastián.-

Risposta accettata

Star Strider
Star Strider il 23 Apr 2015
I would use the reshape function to redefine x(:,1), y(:,2) and y(:,3) as:
X1 = reshape(x(:,1), 6, []);
Y2 = reshape(y(:,2), 6, []);
Y3 = reshape(y(:,3), 6, []);
then:
for k1 = 1:size(X1,2)
W(k1) = trapz(X1(:,k1),min(Y3(:,k1),0)/trapz(X1(:,k1),min(Y3(:,k1),0))
end
There is something wrong with the code you posted, since it doesn’t make sense, so I didn’t run this code. (I assume it produces a scalar.) I will leave you to sort that out.
  4 Commenti
sruizpp
sruizpp il 26 Mag 2015
Greetings, If X1 is reshaped, it is no longer recognized as vector and throws an error while running the code. And yes, my result needs to be a scalar. I need that value each 6 terms.
Regards!
Star Strider
Star Strider il 26 Mag 2015
Did you run the for loop? It treats every column of ‘X1’ (and ‘Y3’) as a separate vector, so there should be no problem.

Accedi per commentare.

Più risposte (1)

Geoff Hayes
Geoff Hayes il 23 Apr 2015
Try setting the step size in your for loop to be 6. Something like the following may work where we assume that both X and Y have 1800 rows.
numRows = size(Y,1);
stepSize = 6;
for k=1:stepSize:numRows
value = trapz(X(k:k+stepSize-1,1),min(Y(k:k+stepSize-1,3),0)/...
trapz(X(k:k+stepSize-1,1),min(Y(k:k+stepSize-1,2),0);
% save value to other matrix
end
Given that stepSize is six, then our k becomes 1, 7, 13, 19, ..., 1795 and we get the correct groupings of six elements of data (since k + stepSize - 1 becomes 6, 12, 18, ..., 1800).
Try the above and see what happens!

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