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

1 次查看(过去 30 天)
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 个评论
Steven Lord
Steven Lord 2019-5-9
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 2019-5-11
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

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by