Store values from while loop into an array

조회 수: 28 (최근 30일)
Jacob Forbes
Jacob Forbes 2021년 4월 2일
댓글: Christopher McCausland 2021년 4월 3일
I'm writing a function that will calculate the distance between two vectors. When trying to store the values from each iteration of the while loop into an array, I'm getting an "Index exceeds the number of array elements (2)." error message. Here's the code:
function [distvect,theta] = calcDistAngle(u,v)
if (length(u) == length(v) && iscolumn(u) == 1 && iscolumn(v) == 1)
n=0;
c = zeros(length(u), 1);
while n <= length(u)
n = n+1;
c(n) = (u(n)-v(n))^2;
end
distvect = sqrt(sum(c(n)));
theta = acos((dot(u,v))/(norm(u)*norm(v)));
else
distvect = -1;
theta = -1;
end
end
Thanks in advance for the help
  댓글 수: 1
jannat alsaidi
jannat alsaidi 2021년 4월 2일
did you mean by (the values from each iteration ) you want to store answer of c in an array? array with what size?

댓글을 달려면 로그인하십시오.

채택된 답변

Christopher McCausland
Christopher McCausland 2021년 4월 2일
Hi Jacob, I am not sure what u and v are suppsosed to look like, if you could give some data that would be great! I have been able to get the code to 'fall over' as you describe using u = [1;2;3;4;5;6]; v = [1;2;3;4;5;6];. This may not process as you would like but its what I had to go on. I have made on modification to you code on line 9 below.
The extra equals meant that you were indexing to the c(end)+1, i.e past then end of 'c' which is what "Index exceeds the number of array elements (2)." is trying to tell you, here is more about that error.
u = [1;2;3;4;5;6];
v = [1;2;3;4;5;6];
[distvect,theta] = calcDistAngle(u,v)
function [distvect,theta] = calcDistAngle(u,v)
if (length(u) == length(v) && iscolumn(u) == 1 && iscolumn(v) == 1)
n=0;
c = zeros(length(u), 1);
while n < length(u) % change from <= to < to keep within the bounds of the array
n = n+1;
c(n) = (u(n)-v(n))^2;
end
distvect = sqrt(sum(c(n)));
theta = acos((dot(u,v))/(norm(u)*norm(v)));
else
distvect = -1;
theta = -1;
end
end
  댓글 수: 2
Jacob Forbes
Jacob Forbes 2021년 4월 2일
Wow such a simple solution I completely overlooked, thank you so much!
Christopher McCausland
Christopher McCausland 2021년 4월 3일
Hi Jacob,
No worries, I am glad I could help. Breakpoints are always your friend to try and understand why your code isn't working as you'd expect.
For furture problems I would also take a look at @DGM below. I am not a fan of while loops as they can be infinite and that causes issues! If a for loop of a predeterimed length can be used instead this can stop a lot of headache for when things go wrong!
Christopher

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

DGM
DGM 2021년 4월 2일
편집: DGM 2021년 4월 2일
You're testing that n<=length(u), but then you immediately increment it. You'd need to adjust your test limit.
Better yet, avoid while loops if you already know the number of iterations you need. It's more concise, and there are fewer things to go wrong.
% iscolumn already returns a logical
if (length(u) == length(v) && iscolumn(u) && iscolumn(v))
c = zeros(length(u), 1);
for n=1:length(u)
c(n) = (u(n)-v(n))^2;
end
distvect = sqrt(sum(c)); % you probably want to find the 2-norm of the whole thing
theta = acos((dot(u,v))/(norm(u)*norm(v)));
else
distvect = -1;
theta = -1;
end

카테고리

Help CenterFile Exchange에서 Direct Search에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by