How can I pair elements in a row of a matrix, but sort the pairs by their second element?

1 view (last 30 days)
In my program, I've called a function to give me a row vector with the number of columns that match the number of pairs of rows from a preestablished matrix. I have to arrange these values in ascending order in a table, but they must correspond to a certain name. For example, my function gives me spring constants, and I've created a matrix with the spring numbers in the first column and their corresponding spring constants in the second column. Sorting this matrix will reassign spring constants to different springs. If I have for example,
[3,k3;
1,k1;
2,k2;
5,k5;
4,k4]
(with kx being the spring constant). How can I make it so that I can arrange them in ascending order without changing the fact that one spring constant always corresponds to one spring?

Accepted Answer

the cyclist
the cyclist on 6 Mar 2013
Use the sortrows() command.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!