MATLAB Answers

Store values from while loop into an array

21 ビュー (過去 30 日間)
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 件のコメント
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

Community Treasure Hunt

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

Start Hunting!

Translated by