Indexing issue- using negative values in for loop calculations.
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Shrishti Yadav
il 30 Ott 2021
Commentato: Shrishti Yadav
il 30 Ott 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 Commenti
Risposta accettata
DGM
il 30 Ott 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(:)]
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!