Indexing issue- using negative values in for loop calculations.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Shrishti Yadav
el 30 de Oct. de 2021
Comentada: Shrishti Yadav
el 30 de Oct. de 2021
I would like to store the values Zx and Zy from the code below in Z but not sure how.
clc;
u = 4
Z = zeros(u*u,1);
cx = 1;
cy = 1;
r = 3;
for j = 1:(u*u)
for x = -u:u
for y = -u:u
theta = atan2((y-cy) ,(x-cx));
Zx = cx-r*cos(theta)
Zy = cy - r*sin(theta)
Z(j,1) = (Zx);
Z(j,2) = (Zy);
end
end
end
0 comentarios
Respuesta aceptada
DGM
el 30 de Oct. de 2021
It's not clear why you're overwriting your results and then repeating the process 16 times anyway. If you want Z for all x and all y, then the outer loop doesn't do anything.
u = 4;
Z = zeros(u*u,1);
cx = 1;
cy = 1;
r = 3;
x = -u:u;
y = (-u:u).';
theta = atan2((y-cy) ,(x-cx));
Zx = cx - r*cos(theta)
Zy = cy - r*sin(theta)
If you want Zx and Zy to be reshaped as column vectors, figure out how you want them reshaped:
Z = [Zx(:) Zy(:)]
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!