Shifting row and column of a matrix with minimum and maximum to the top and right repectively?
1 view (last 30 days)
Show older comments
For a matrix with dimensions greater than 4 X 4. How to move the row with minimum sum to the top and the column with maximum sum to the right, by writing a function, including loops and conditions.
4 Comments
Answers (2)
KALYAN ACHARJYA
on 30 Oct 2018
Edited: KALYAN ACHARJYA
on 30 Oct 2018
a=input('Enter the Matrix using []: ');
[rows colm]=size(a);
for i=1:rows
sum_row(i)=sum(a(i,:));
end
[M,I]=min(sum_row);
b=a(1,:);
a(1,:)=a(I,:);
a(I,:)=b;
disp(a);
Command Window:
Enter the Matrix using []: [2 3 4; 5 0 7;0 2 -3]
0 2 -3
5 0 7
2 3 4
1 Comment
KALYAN ACHARJYA
on 30 Oct 2018
Edited: KALYAN ACHARJYA
on 30 Oct 2018
You can do the same in a more simpler way (exchange row), but this code is easy to understand.
Stephen23
on 30 Oct 2018
Edited: Stephen23
on 30 Oct 2018
A simple MATLAB way (no loops, just basic indexing):
>> M = randi(9,7,5)
M =
5 1 4 1 9
7 8 2 6 4
3 1 4 8 3
6 3 4 6 2
3 1 4 7 6
5 7 6 8 6
2 2 9 6 5
>> S = sum(M,2)
S =
20
27
19
21
21
32
24
>> [~,X] = min(S)
X = 3
>> M = M([X,1:X-1,X+1:end],:)
M =
3 1 4 8 3
5 1 4 1 9
7 8 2 6 4
6 3 4 6 2
3 1 4 7 6
5 7 6 8 6
2 2 9 6 5
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!