create a magic 5x5 matrix and remove the diagonal elements to generate a new 5x4 matrix. provide 2 different solutions

1 visualización (últimos 30 días)
Create a magic 5x5 matrix and remove the diagonal elements to generate a new 5x5 matrix.Provide 2 different solutions.
please can you provide explanations for why you do each step.
  2 comentarios
Steven Lord
Steven Lord el 9 de Mayo de 2019
This sounds like a homework assignment. If it is, show what you've done to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to offer guidance.
If you're not sure even where to start because you aren't sure how to work with MATLAB, I strongly recommend going through the MATLAB Onramp free tutorial which will help you learn the essentials of MATLAB programming.
If you're unclear about the question as a whole, I recommend contacting your professor and/or the teaching assistant for your course for clarification.
Zahgam Saleem
Zahgam Saleem el 11 de Mayo de 2019
I have started it and I have given all the questions that i have asked a go. For this question I managed to complete it by myself. but i still needed help with my assignment.
Question 1
Solution 1: >> a=magic(5)
a =
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
>> a(1:6:end)=[]
a =
Columns 1 through 8
23 4 10 11 24 6 12 18
Columns 9 through 16
1 7 19 25 8 14 20 2
Columns 17 through 20
15 16 22 3
>> reshape(a,5,4)
ans =
23 6 19 2
4 12 25 15
10 18 8 16
11 1 14 22
24 7 20 3
Solution 2:
>> a=magic(5)
a =
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
>> a=a-diag(diag(a))
a =
0 24 1 8 15
23 0 7 14 16
4 6 0 20 22
10 12 19 0 3
11 18 25 2 0
>> a(a==0)=[]
a =
Columns 1 through 11
23 4 10 11 24 6 12 18 1 7 19
Columns 12 through 20
25 8 14 20 2 15 16 22 3
>> reshape(a,5,4)
ans =
23 6 19 2
4 12 25 15
10 18 8 16
11 1 14 22
24 7 20 3

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by