Attempted to access x(5); index out of bounds because numel(x)=4.

1 回表示 (過去 30 日間)
lotus whit
lotus whit 2016 年 8 月 2 日
コメント済み: Image Analyst 2016 年 8 月 2 日
could you please to help me , i have this error when i tried to run this code which calculate distance between two points groups
x = [2,3,4,5];
y = [4,4,5,4];
s_x = size(x);
d2 = (s_x(1,2));
d = zeros(1,4)
for ii=1:1:d2
d(ii) = sqrt((x(ii+1)-x(ii))^2+(y(ii+1)-y(ii))^2);
end
any one have any idea about solution?

回答 (2 件)

Image Analyst
Image Analyst 2016 年 8 月 2 日
It looks like you're trying to do this:
x = [2,3,4,5];
y = [4,4,5,4];
deltax = diff(x)
deltay = diff(y)
distances = sqrt(deltax .^2 + deltay .^2)

James Tursa
James Tursa 2016 年 8 月 2 日
編集済み: James Tursa 2016 年 8 月 2 日
The error is caused by ii iterating from 1 to 4, but you use ii+1 (=5 at last iteration) as an index into the x and y vectors which are only of length 4. For your example, what would you like the desired output to be? I.e., what would the desired values of d be for your example?
  2 件のコメント
lotus whit
lotus whit 2016 年 8 月 2 日
Thank you about your fast response, i want to calculate the distance between each point in x,y vectors , for example point1(x1,y1) , point2(x2,y2), i need to calculate the distance between point1 and point2 then calculate distance point2 and point3 then point3 and point4 and so on dependent on length of vector(x) and vector(y). thank you again
Image Analyst
Image Analyst 2016 年 8 月 2 日
That's what my code in my Answer does.

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by