Save a loop iteration into a ZEROS array

21 visualizzazioni (ultimi 30 giorni)
Marcelo Gonzalez
Marcelo Gonzalez il 18 Set 2021
Risposto: KSSV il 19 Set 2021
I am trying to run a cycle inside a cycle, the inner cicle gets all the data for the rist row while the outer cycle changes to the next row and it keeps going until the 100x100 array is filled with the info I got from running each loop. But all I get is a 100x100 array of zeros. Can anyone help me out?
Code:
A = 10;
B = 5;
i = sqrt(-1);
Array = zeros(100,100);
a = 1;
b = 1;
for y = 0:0.1:10
for x = 0.1:0.1:10
fXy = A.*exp(-(x.^2 + y.^2)/(B^2)).*exp(i.*atan(y/x));
Array(b,a) = fXy;
a = a + 1;
end
b = b + 1;
end

Risposte (1)

KSSV
KSSV il 19 Set 2021
Note that Array is not all zeros. It has complex numbers, so when you are printing you feel like they are zeros. Also you need not to use loop. You can proceed as below:
x = 0.1:0.1:10 ;
y = 0:0.10:10 ;
[x,y] = meshgrid(x,y) ;
Z = A.*exp(-(x.^2 + y.^2)/(B^2)).*exp(1i.*atan(y./x));

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by