Attempted to access x(5); index out of bounds because numel(x)=4.
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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?
0 commentaires
Réponses (2)
Image Analyst
le 2 Août 2016
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)
0 commentaires
James Tursa
le 2 Août 2016
Modifié(e) : James Tursa
le 2 Août 2016
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 commentaires
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!