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

1 回表示 (過去 30 日間)
sruizpp
sruizpp 2015 年 4 月 22 日
コメント済み: Star Strider 2015 年 5 月 26 日
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!
  1 件のコメント
sruizpp
sruizpp 2015 年 4 月 23 日
Very Kind, I'll check this one. Thx!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by