MATLAB: Nested for Loop needed

forloopnested

Hello,
Could anyone enlighten me as to how to produce the for loops required to perform the following:
2 x 3
2 x 4
2 x 5
3 x 4
3 x 5
4 x 5
The numbers actually refer to the rows of a matrix.
I need a loop which starts with the second row, and multiplies it sequentially with every row beneath it, then moves on to the row below and does the same.
Could anyone explain how this would be done for a matrix with 5 rows as above? I'm sure I could generalise it to n rows, but I have been struggling with this for some time now.
Kind regards,
Tom

Best Answer

Try this:
clc;
workspace; % Make sure the workspace panel is showing.
m = magic(5)
outMatrix = m; % Initialize.
for row = 2 : size(m, 1)
newRow = prod(m(row:end, :), 1);
outMatrix(row, :) = newRow;
end
% Print to command window:
outMatrix
In the command window:
m =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
outMatrix =
17 24 1 8 15
10120 6480 43225 11760 9504
440 1296 6175 840 594
110 216 475 42 27
11 18 25 2 9