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.-
댓글 수: 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!