MATLAB: How can i allocate results in matrix with for loop

allocationforfor loopmatgrix

i tried store my results of exp()… in matrix with for loop.
if you guys how would you do this?

Best Answer

This is MATLAB, so just get rid of the pointless loops entirely:
>> w = 0:4;
>> t = 0:9;
>> y = exp(t.*w(:).*1i)
y =
Columns 1 through 5:
1.00000 + 0.00000i 1.00000 + 0.00000i 1.00000 + 0.00000i 1.00000 + 0.00000i 1.00000 + 0.00000i
1.00000 + 0.00000i 0.54030 + 0.84147i -0.41615 + 0.90930i -0.98999 + 0.14112i -0.65364 - 0.75680i
1.00000 + 0.00000i -0.41615 + 0.90930i -0.65364 - 0.75680i 0.96017 - 0.27942i -0.14550 + 0.98936i
1.00000 + 0.00000i -0.98999 + 0.14112i 0.96017 - 0.27942i -0.91113 + 0.41212i 0.84385 - 0.53657i
1.00000 + 0.00000i -0.65364 - 0.75680i -0.14550 + 0.98936i 0.84385 - 0.53657i -0.95766 - 0.28790i
Columns 6 through 10:
1.00000 + 0.00000i 1.00000 + 0.00000i 1.00000 + 0.00000i 1.00000 + 0.00000i 1.00000 + 0.00000i
0.28366 - 0.95892i 0.96017 - 0.27942i 0.75390 + 0.65699i -0.14550 + 0.98936i -0.91113 + 0.41212i
-0.83907 - 0.54402i 0.84385 - 0.53657i 0.13674 + 0.99061i -0.95766 - 0.28790i 0.66032 - 0.75099i
-0.75969 + 0.65029i 0.66032 - 0.75099i -0.54773 + 0.83666i 0.42418 - 0.90558i -0.29214 + 0.95638i
0.40808 + 0.91295i 0.42418 - 0.90558i -0.96261 + 0.27091i 0.83422 + 0.55143i -0.12796 - 0.99178i
If you really want to use pointless nested loops, then try this:
y = nan(5,10);
for t = 0:4
for w = 0:9
y(t+1,w+1) = exp(t*w*1i)
end
end