How do I create the following matrices using a for loop

4 visualizaciones (últimos 30 días)
Zahgam Saleem
Zahgam Saleem el 9 de Mayo de 2019
Respondida: Udoi el 22 de Jun. de 2022
Create the following matrices using a for loop:
Matrix 1: 5 3 6 10 9
6 4 7 11 10
7 5 8 12 11
8 6 9 13, 12
9 7 10 14 13
Matrix 2: 1 1/2 1/3 1/4 1/5
1/2 1/3 1/4 1/5 1/6
1/3 1/4 1/5 1/6 1/7
1/4 1/5 1/6 1/7 1/8
1/5 1/6 1/7 1/8 1/9
Please can you explain to me the steps involved in order to do this task.
Thanks in advance
  2 comentarios
Alex Mcaulley
Alex Mcaulley el 9 de Mayo de 2019
Are you asking for all your homework? You should follow the Steven's comment in your other question:
Stephen23
Stephen23 el 9 de Mayo de 2019
"Please can you explain to me the steps involved in order to do this task."
Sure, that is easy:
  1. Do the introductory tutorials: https://www.mathworks.com/help/matlab/getting-started-with-matlab.html
  2. Read the documentation for for and colon.
  3. Use any other resources, e.g. look at examples online, search this forum, etc.
  4. Practice, make mistakes, learn ....

Iniciar sesión para comentar.

Respuestas (1)

Udoi
Udoi el 22 de Jun. de 2022
Since the dimensions of each matrix is 5*5,we can easily create the matrices without any for loop.
However ,since we are specified to use for loop here,the code will be overhwhelmingly long becuase we would have to deal with 5*5=25 individual cells of the matrix separately.
However,an interesting point of observation for matrix 1 is that ,each column starts with a partcular number or value and the corresponding values going down the row for that particular column are just incremented by 1.So we can create a 5*5 matrix at first,and then initialize the first row with the respective values as given.Now we can iterate from the second row till the last row,where each cell's value is basically the upper cell's(i.e the cell in the upper row of the same column) value incremented by 1.
So,this might certainly be a pattern finding question which is meant to strengthen your observation skills.
Code snippet for matrix 1:
a=zeros(5,5);
for i=1:1
for j=1:5
if(j==1)
a(i,j)=5;
end
if(j==2)
a(i,j)=3;
end
if(j==3)
a(i,j)=6;
end
if(j==4)
a(i,j)=10;
end
if(j==5)
a(i,j)=9;
end
end
end
for i=2:5
for j=1:5
a(i,j)=a(i-1,j)+1;
end
end
If you are stuck on how to use for loops,for the purpose,I would suggest you to .
  1. Do the introductory tutorials.Follow the link https://www.mathworks.com/help/matlab/getting-started-with-matlab.html

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by