How do I Loop a code every 6 rows until the end?
2 次查看(过去 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.-
0 个评论
采纳的回答
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 个评论
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
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 Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!