Use equation in for loop

2 次查看(过去 30 天)
triztyme
triztyme 2016-5-3
评论: triztyme 2016-5-4
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
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?
triztyme
triztyme 2016-5-4
编辑:triztyme 2016-5-4
Here is a sample of the data
(x) (y)
0.000 /0.000
0.023 /64.792
0.072 /178.808
0.122 /213.286
0.172 /225.909
0.222 /224.443
.......
so it should go
total=toal+(0.000+64.792)*(.023-0.000)
then to the next row
total=total+(64.792+178.808)*(0.072-0.023)
and keep looping through all data

请先登录,再进行评论。

采纳的回答

MHN
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
  1 个评论
triztyme
triztyme 2016-5-4
thank you! I am required to use a for loop, if it were up to me I would use your other example

请先登录,再进行评论。

更多回答(2 个)

Walter Roberson
Walter Roberson 2016-5-4
hint: x(m), x(m+1)

CS Researcher
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)));

类别

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