Indexing issue- using negative values in for loop calculations.
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
DGM
on 30 Oct 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(:)]
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!