how to create a matrix (3*3) from 1 to 9 by using for loop

45 views (last 30 days)
How to create a matrix (3*3) from 1 to 9 by using For loop like:
1 2 3
4 5 6
7 8 9
.................
for i=1:3 %row%
for j=1:3 %column%
a(i,j)= input('a=');
end
end
disp (a)
..................
I wrote this code, but I don't want to input the digits from 1 to 9 by keyboard. I want the computer enter these digits automatically by using For loop

Accepted Answer

BN
BN on 22 Dec 2019
I think you don't need any for loop to achieve this goal. try this code below:
reshape( 1:9, 3, 3) .'
  8 Comments
prog computer
prog computer on 22 Dec 2019
clc,clear
k=1;
for i=1:3
for j = 1:3
a(i,j)=k;
k=k+1;
end
end
disp (a)

Sign in to comment.

More Answers (1)

Muddasir Muddasir
Muddasir Muddasir on 17 Mar 2022
for i=1:3 for j=1:3 a(i, j) =input('a=') : end end disp(a)

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!