Shifting row and column of a matrix with minimum and maximum to the top and right repectively?

1 view (last 30 days)
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
JAYEN PILLAY
JAYEN PILLAY on 23 Nov 2019
@Madhan ravi, yeah it is, indeed ! And thanks to those who constructiely replied. Now I understand code better, and I think twice before I use loops

Sign in to comment.

Answers (2)

KALYAN ACHARJYA
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

Stephen23
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
  1 Comment
JAYEN PILLAY
JAYEN PILLAY on 12 Nov 2018
Edited: JAYEN PILLAY on 12 Nov 2018
Thank you; let's say I want to index the rows in such a way that the function can output the minimum row's initial position in the matrix. Can I do it by using a for loop and continue?

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!