Use equation in for loop
조회 수: 5 (최근 30일)
이전 댓글 표시
I have a 2d array that I'm reading in that is 100x2. I have the following equation that needs to loop through and calculate starting at the next row
total=total+(y1+y2)*(x1-x2)
Here's what I've started with, I'm just not sure how to set it up
[nrows,ncols] = size(data);
for m=1:1:nrows
for n=1:1:ncols
total = total+
댓글 수: 2
Walter Roberson
2016년 5월 4일
What are x1, x2, y1, y2? How do they relate to the content of the image? How do they relate to the row or column numbers?
채택된 답변
MHN
2016년 5월 4일
data = rand(100,2); % just for an example
[nrows,ncols] = size(data);
total = 0;
for m=1:nrows-1
total = total+ (data(m,2)+data(m+1,2))*(data(m,1)+data(m+1,1));
end
추가 답변 (2개)
CS Researcher
2016년 5월 4일
편집: CS Researcher
2016년 5월 4일
You can avoid the for loop by:
data = rand(100,2); % just for an example
N = size(data,1);
total = sum((data(1:N-1,2) + data(2:N,2)) .* (data(1:N-1,1) + data(2:N,1)));
댓글 수: 0
참고 항목
카테고리
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!