MATLAB对2D和3D矩阵进行排序并通过索引进行访问 [英] MATLAB Sort 2D and 3D matrix and access through index

查看:188
本文介绍了MATLAB对2D和3D矩阵进行排序并通过索引进行访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们说你有1D矩阵

a = rand(1,5);
[sa i] = sort(a);

然后 sa a(i)是相同的。但是,如果矩阵的大小增加

then sa and a(i) are the same. However, if the size of the matrix increases

a = rand(3,4);
[sa i] = sort(a);

然后 sa a(i)不一样。当我尝试按照第三维对3D矩阵进行排序时,也会发生同样的情况。

then sa and a(i) are not the same. And the same happens when I'm trying to sort a 3D matrix by its third dimension.

如何通过索引 i a 的值$ C>?或者换句话说,如何计算 sa = a(X) X 应该是什么?

How can I access the values of a through the index i? Or in other words how can I compute the sa=a(X), what X should be?

编辑:

感谢您的解决方案。但是,当您更改要排序的维度时,它们不起作用。不过,我接受了这个想法并用它来构建一般形式。

Thanks for the solutions. However, they don't work when you change the dimension to sort by. Nevertheless, I pick up the idea and use it to build a general form.

算法的作用是构建矩阵的索引。 MATLAB逐列索引单元格。因此,指数由

What the algorithm is doing is to build the indexes of the matrix. MATLAB index the cells column wise. Therefore, the index is given by

idx = r + (c-1)*ROWS + (p-1)*ROWS*COLS

其中, idx 是指数, r 是行位置, c 是列位置, p 是页面位置。

where, idx is the index, r is the row position, c is the column position, and p is the page position.

因此,如果我们按第一维排序(正常 sort(a))结果索引是列中的位置;如果我们在第二维中排序,结果索引就是行中的位置;如果我们在第三维中排序,结果索引就是页面位置。这就是说,它只能为给定的情况生成行和列:

Therefore, if we sort in the first dimension (normal sort(a)) the result index is the position in the columns; if we sort in the second dimension, the result index is the position in the rows; and if we sort in the third dimension, the result index is the page position. This being said, it only last to produce the rows and cols for the given case:

r = repmat((1:rows)',[1 cols pages]);
c = repmat(1:cols,[rows 1 pages]);

在给出的解决方案中解释了第一维中的排序。然后,让我们在二维数组的第二维(行方式)中排序

Sorting in the first dimension is explained in the solutions given. Then, lets sort in the second dimension (row wise) of a two dimensional array

a = rand(4,5);
[rows cols pages] = size(a);
R = repmat((1:rows)',[1 cols pages]);
[sa idx] = sort(a,2);
nIdx = R + (idx-1)*rows;
isequal(sa,a(nIdx))

现在,如果我们使用相同的想法为了在第三维排序(页面方式),我们需要做

Now, if we use the same idea for sorting in the third dimension (page wise), we need to do

a = rand(4,5,3);
[rows cols pages] = size(a);
R = repmat((1:rows)',[1 cols pages]);
C = repmat(1:cols,[rows 1 pages]);
[sa idx] = sort(a,3);
nIdx = R + (C-1)*rows + (idx-1)*rows*cols;
isequal(sa,a(nIdx))

并且可以使用相同的逻辑将其扩展到N维。
感谢您的帮助,您点亮了道路。 :)

And the same logic can be used to extend it to N dimensions. Thanks for your help, you en light the way. :)

推荐答案

[sa,i] = sort(a)返回每列的有序索引。您只需要获得矩阵的正确线性指数。因此,对于2D矩阵,

[sa, i]=sort(a) returns the ordered indices for each column. You just need to get the correct linear indices for the matrix. So, for a 2D matrix,

A=rand(3,4);
[rows,cols]=size(A);
[B,index]=sort(A,1);
correctedIndex=index+repmat(0:cols-1,rows,1)*rows;

现在测试一下:

A =

    0.9572    0.1419    0.7922    0.0357
    0.4854    0.4218    0.9595    0.8491
    0.8003    0.9157    0.6557    0.9340

B =

    0.4854    0.1419    0.6557    0.0357
    0.8003    0.4218    0.7922    0.8491
    0.9572    0.9157    0.9595    0.9340

A(correctedIndex)

ans =

    0.4854    0.1419    0.6557    0.0357
    0.8003    0.4218    0.7922    0.8491
    0.9572    0.9157    0.9595    0.9340

这篇关于MATLAB对2D和3D矩阵进行排序并通过索引进行访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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