假设我有一个n行m列矩阵A,并且我想根据某个特定行的排序对m中的每一列重新排序。
例如,如果我接受顺序(A [,k]),那就给出了k列中元素的数字或字母顺序。 我现在想要根据这些排名对矩阵A中的每一列进行排序,以便每行中的元素1 ... n被排序以对应于列k中的元素1 ... n(按等级)。 是否有一种简单的方法可以在不循环所有列的情况下执行此操作?
Suppose that I have an n row, m column matrix A, and I want to reorder every column in m according to the sorting of some specific row.
For instance, if I take order(A[,k]), that gives me the numeric or alphabetical order of elements in column k. I now want to sort every column in matrix A according to those rankings, so that elements 1...n in every row are ordered to correspond to elements 1...n (by rank) in column k. Is there a simple way to do this without looping over all columns?
最满意答案
只需使用:
A[order(A[,k]),]例如:
set.seed(21) A <- matrix(rnorm(50),10,5) A[order(A[,1]),]Just use:
A[order(A[,k]),]For example:
set.seed(21) A <- matrix(rnorm(50),10,5) A[order(A[,1]),]更多推荐
发布评论