Is there any one who can help for the following matrix problem

I have 3X6 matrix:
M=[15 18 14 19 17 14;
30 3 8 14 1 5;
7 16 13 11 20 2] ;
It is required to sort them using only one of the smallest value in the column (i.e. the column which contain the smallest value should be first column in the matrix without change the order of original column). The sorted output should be displayed as:
M=[17 14 18 19 15 14;
1 5 3 4 30 8;
20 2 16 11 7 13];
Since the value 1 is the smallest value in column 5 at the original matrix, it comes in the first column for sorted output matrix.

4 Comments

I don't understand your sorting algorithm. Like for the first row, 17 is not the smallest value, 14 is. Yet you put 17 first. Then I'm not sure how the others are sorted. Like it's not sorted in ascending or descending order. It's not even circularly shifted because 14 comes after 18 in the original matrix, but in the answer 19 comes after 18. Please explain your rule(s) better.
Hi Sir,the sorting algorithm is based on a single cell value in the column. As you observe in the required output, if the column contain smallest value then it become in the first column of the matrix and so on. For example [17; 1; 20] are column values in the first column of the matrix in the required output matrix.This shows that 1 is the smallest value in that column.
@Keyre: It matters for the kind of answering, if this is a homework or not. Please be so kind and clarify this.
My guess is that it is unlikely to be a homework assignment. If it is, I hope my solution won't get credit. It only takes two lines of code using built-in MATLAB functions.

Sign in to comment.

 Accepted Answer

I think this is the way to do it. Your expected output has typo in it.
M=[15 18 14 19 17 14;
30 3 8 14 1 5;
7 16 13 11 20 2] ;
MinM=min(M);
[~,Index]=sort(MinM);
NewM=M(:,Index)
NewM =
17 14 18 15 14 19
1 5 3 30 8 14
20 2 16 7 13 11

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!