MATLAB-根据向量的排序方式对矩阵进行排序 [英] MATLAB - Sort a matrix based off how a vector is sorted

查看:414
本文介绍了MATLAB-根据向量的排序方式对矩阵进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何我在MATLAB中针对一列对二维数组进行排序?
将一个矩阵与另一个矩阵排序

Possible Duplicate:
How can I sort a 2-D array in MATLAB with respect to one column?
Sort a matrix with another matrix

我有一个429值的矢量"A"和一个429x200值的矩阵"B". A和B中的行共享相同的索引.我的向量"A"包含值1:1:429,但在整个向量中它们是随机排序的.我想对A进行重新排序,以便它按从1到429的顺序进行索引,并且我还想对矩阵'B'中的行进行与新排序的'A'相同的排序.

I have a vector 'A' of 429 values and a matrix 'B' of 429x200 values. Rows in A and B are share the same indices. My vector 'A' contains values 1:1:429 but they are randomly ordered throughout the vector. I want to reorder A so that it indexes in order from 1 to 429 and I also want to sort the rows in matrix 'B' in the same order as the newly sorted 'A'.

可以在没有for循环的情况下快速轻松地完成此操作吗?

Can this be done quick and easy without a for-loop?

下面是一个说明我的观点的例子:

Here's an example to illustrate my point:

A =
    5
    3
    1
    2
    4


 B =
    3   7   0   4   6
    1   2   5   0   8
    4   0   2   0   0
    3   0   1   0   5
    2   2   3   4   4


sortedA = 

1
2
3
4
5

sortedB =

4   0   2   0   0
3   0   1   0   5
1   2   5   0   8
2   2   3   4   4
3   7   0   4   6

谢谢大家!

推荐答案

示例数据:

A = [ 5, 3, 1, 2, 4 ]';

B = [ 3, 7, 0, 4, 6; 1, 2, 5, 0, 8; 4, 0, 2, 0, 0; 3, 0, 1, 0, 5; 2, 2, 3, 4, 4 ]

对矩阵进行排序:

[sortedA,IX] = sort(A);

sortedB = B(IX,:);

sortedA =
 1
 2
 3
 4
 5

sortedB =
 4     0     2     0     0
 3     0     1     0     5
 1     2     5     0     8
 2     2     3     4     4
 3     7     0     4     6

这篇关于MATLAB-根据向量的排序方式对矩阵进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆