Indexing issue- using negative values in for loop calculations.
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
DGM
2021-10-30
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(:)]
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!