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

1 次查看(过去 30 天)
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.-

采纳的回答

Star Strider
Star Strider 2015-4-23
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 个评论
sruizpp
sruizpp 2015-5-26
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 2015-5-26
Did you run the for loop? It treats every column of ‘X1’ (and ‘Y3’) as a separate vector, so there should be no problem.

请先登录,再进行评论。

更多回答(1 个)

Geoff Hayes
Geoff Hayes 2015-4-23
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!

类别

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