use nested for-loops to generate a matrix (without typing the numbers explicitly)
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sajid Sarwar
am 16 Nov. 2019
Bearbeitet: KALYAN ACHARJYA
am 16 Nov. 2019
I want to use nested for-loops to generate a matrix that has elements shown below (without typing the numbers explicitly)
I need help in developing logic.
12 8 4 0 -4
14 10 6 2 -2
16 12 8 4 0
A=[ ];
for i=1:3
for j=1:5
A(i,j)=? % need help in developing logic.
end
end
0 Kommentare
Akzeptierte Antwort
KALYAN ACHARJYA
am 16 Nov. 2019
Bearbeitet: KALYAN ACHARJYA
am 16 Nov. 2019
I want to use nested for-loops to generate a matrix
Multiple ways you can do that, as you asked for:
init_val=12;
for i=1:3
for j=1:5
A(i,j)=init_val;
init_val=init_val-4;
end
init_val=A(i,1)+2;
end
A
OR
array_data=12:-4:-4; % Given
A=[array_data;array_data+2;array_data+4]
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!