Store values from while loop into an array

29 visualizaciones (últimos 30 días)
Jacob Forbes
Jacob Forbes el 2 de Abr. de 2021
Comentada: Christopher McCausland el 3 de Abr. de 2021
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 comentario
jannat alsaidi
jannat alsaidi el 2 de Abr. de 2021
did you mean by (the values from each iteration ) you want to store answer of c in an array? array with what size?

Iniciar sesión para comentar.

Respuesta aceptada

Christopher McCausland
Christopher McCausland el 2 de Abr. de 2021
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 comentarios
Jacob Forbes
Jacob Forbes el 2 de Abr. de 2021
Wow such a simple solution I completely overlooked, thank you so much!
Christopher McCausland
Christopher McCausland el 3 de Abr. de 2021
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

Iniciar sesión para comentar.

Más respuestas (1)

DGM
DGM el 2 de Abr. de 2021
Editada: DGM el 2 de Abr. de 2021
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

Categorías

Más información sobre Direct Search en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by