How can i allocate results in matrix with for loop?

1 view (last 30 days)
i tried store my results of exp()... in matrix with for loop.
if you guys how would you do this?

Accepted Answer

Stephen23
Stephen23 on 9 Dec 2018
Edited: Stephen23 on 9 Dec 2018
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

More Answers (1)

madhan ravi
madhan ravi on 9 Dec 2018
y=zeros(6,10); %change this
ctr=1; %before loop
y(ctr,:)=.... change this part
ctr=ctr+1; % add this before last end (after the first end)- > inbetween two ends
  1 Comment
madhan ravi
madhan ravi on 9 Dec 2018
If my answer solved your problem make sure to accept the answer else let know.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!