storing loop values in a matrix

3 views (last 30 days)
I have a column 10x1. Now i have to perform loop operation 5 times (i=1:5). I want to save the results of each loop so that the final matrix should be 10x5. Pls suggest the code.

Accepted Answer

Walter Roberson
Walter Roberson on 25 Jul 2021
N = 10;
iters = 5;
column = randi(9,N,1)
column = 10×1
8 3 2 8 5 6 2 7 3 4
results_of_loop = zeros(N, iters);
for K = 1 : iters
results_of_loop(:,K) = column.^K / factorial(K);
end
results_of_loop
results_of_loop = 10×5
8.0000 32.0000 85.3333 170.6667 273.0667 3.0000 4.5000 4.5000 3.3750 2.0250 2.0000 2.0000 1.3333 0.6667 0.2667 8.0000 32.0000 85.3333 170.6667 273.0667 5.0000 12.5000 20.8333 26.0417 26.0417 6.0000 18.0000 36.0000 54.0000 64.8000 2.0000 2.0000 1.3333 0.6667 0.2667 7.0000 24.5000 57.1667 100.0417 140.0583 3.0000 4.5000 4.5000 3.3750 2.0250 4.0000 8.0000 10.6667 10.6667 8.5333
surf(results_of_loop)

More Answers (0)

Categories

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

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!