How does indexing work when sorting a matrix?

4 views (last 30 days)
My intuition is clearly off on this and any help is appreciated.
For a vector, if you sort the vector and find the indices of the sorted vector, you can reproduce the sorted vector by indexing the original vecor using the sorted indices:
r = rand(10,1);
[rsort,indsort] = sort(r);
r(indsort) % This will equal rsort exactly
rsort - r(indsort) % this will equal zero everywhere
However, strangely, this same logic doesn't seem to work when sorting matrices:
R = rand(10,10);
[Rsort,indsort] = sort(R);
R(indsort) %This does NOT match Rsort
Rsort - R(indsort) % This does NOT equal zero everyone, only in the first column
Obviously there is something really simply going on here, but I can't figure it out. Any help is appreciated.
  1 Comment
Stephen23
Stephen23 on 20 Aug 2024
"However, strangely, this same logic doesn't seem to work when sorting matrices"
Perhaps this helps understand what is going on:
R = rand(10,10);
[Rsort,indsort] = sort(R);
for k = 1:size(R,2)
X = indsort(:,k);
R(X,k) - Rsort(:,k)
end
ans = 10x1
0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 10x1
0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 10x1
0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 10x1
0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 10x1
0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 10x1
0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 10x1
0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 10x1
0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 10x1
0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 10x1
0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Sign in to comment.

Answers (3)

Voss
Voss on 20 Aug 2024
For matrix R, sort(R) sorts each column and indsort are row indices.
To reproduce the sorted matrix, you can convert those row indices into linear indices. Here are a few ways to do that:
R = rand(10,10)
R = 10x10
0.3822 0.3039 0.3901 0.6018 0.8021 0.5926 0.0603 0.1671 0.2621 0.5227 0.2526 0.2543 0.4592 0.6546 0.6726 0.1193 0.8220 0.7939 0.6572 0.2299 0.5452 0.0107 0.6738 0.6339 0.6675 0.6142 0.0388 0.5753 0.4326 0.4067 0.1317 0.1817 0.2517 0.5425 0.2303 0.3802 0.7924 0.7668 0.8333 0.8137 0.3915 0.1373 0.5168 0.4309 0.8889 0.0126 0.7957 0.2650 0.3544 0.9291 0.5511 0.8038 0.7838 0.3338 0.9130 0.1119 0.7811 0.4130 0.3063 0.2598 0.2918 0.7141 0.2002 0.9318 0.4224 0.6488 0.9882 0.9189 0.7345 0.1722 0.4971 0.7946 0.9597 0.7124 0.5456 0.8197 0.0367 0.0661 0.6479 0.7692 0.0686 0.0876 0.2461 0.0902 0.5496 0.0117 0.1257 0.7602 0.9223 0.7158 0.2146 0.2000 0.5664 0.7364 0.0102 0.9625 0.8238 0.8660 0.8919 0.6220
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[Rsort,indsort] = sort(R)
Rsort = 10x10
0.0686 0.0107 0.2002 0.0902 0.0102 0.0117 0.0367 0.0661 0.2621 0.1722 0.1317 0.0876 0.2461 0.3338 0.2303 0.0126 0.0388 0.1671 0.3063 0.2299 0.2146 0.1373 0.2517 0.4309 0.4224 0.1119 0.0603 0.2650 0.3544 0.2598 0.2526 0.1817 0.3901 0.5425 0.5456 0.1193 0.1257 0.4130 0.4326 0.4067 0.2918 0.2000 0.4592 0.6018 0.5496 0.3802 0.7811 0.5753 0.6479 0.5227 0.3822 0.2543 0.5168 0.6339 0.6675 0.5926 0.7924 0.7602 0.6572 0.6220 0.3915 0.3039 0.5664 0.6546 0.6726 0.6142 0.7957 0.7668 0.7345 0.7158 0.4971 0.7141 0.6738 0.7124 0.8021 0.6488 0.8220 0.7939 0.8333 0.7692 0.5452 0.7946 0.7838 0.7364 0.8889 0.8197 0.8238 0.8660 0.8919 0.8137 0.5511 0.8038 0.9597 0.9318 0.9130 0.9625 0.9882 0.9189 0.9223 0.9291
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
indsort = 10x10
9 3 7 9 10 9 8 8 1 7 4 9 9 6 4 5 3 1 6 2 10 5 4 5 7 6 1 5 5 6 2 4 1 4 8 2 9 6 3 3 7 10 2 1 9 4 6 3 8 1 1 2 5 3 3 1 4 9 2 10 5 1 10 2 2 3 5 4 7 9 8 7 3 8 1 7 2 2 4 8 3 8 6 10 5 8 10 10 10 4 6 6 8 7 6 10 7 7 9 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[m,n] = size(R);
% one way to get the linear indices from the row indices:
idx = indsort+(0:n-1)*m
idx = 10x10
9 13 27 39 50 59 68 78 81 97 4 19 29 36 44 55 63 71 86 92 10 15 24 35 47 56 61 75 85 96 2 14 21 34 48 52 69 76 83 93 7 20 22 31 49 54 66 73 88 91 1 12 25 33 43 51 64 79 82 100 5 11 30 32 42 53 65 74 87 99 8 17 23 38 41 57 62 72 84 98 3 18 26 40 45 58 70 80 90 94 6 16 28 37 46 60 67 77 89 95
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% another way:
% idx = sub2ind([m,n],indsort,repmat(1:n,m,1))
% another way:
% idx = sub2ind([m,n],indsort,(1:n)+zeros(m,1))
% with linear indexing, the sorted matrix is reproduced:
Rsort-R(idx)
ans = 10x10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

ScottB
ScottB on 20 Aug 2024
For matrices, ‘sort’ orders the elements within columns.

Mario Malic
Mario Malic on 20 Aug 2024
Hi,
R = rand(10,10)
R = 10x10
0.8288 0.3864 0.9078 0.3396 0.0713 0.9826 0.4747 0.7265 0.3330 0.3868 0.4524 0.4739 0.1924 0.6879 0.0571 0.8137 0.2290 0.3599 0.4431 0.2071 0.3368 0.7460 0.9924 0.0108 0.5101 0.5442 0.8826 0.9589 0.4309 0.1135 0.9472 0.6128 0.1313 0.4831 0.0055 0.6986 0.2818 0.4825 0.5604 0.4890 0.3230 0.9389 0.3916 0.7357 0.4607 0.0714 0.1820 0.1908 0.6452 0.4623 0.3826 0.5543 0.5639 0.4370 0.8388 0.6972 0.7228 0.4804 0.8008 0.2282 0.8786 0.9003 0.7716 0.9740 0.0975 0.7926 0.2955 0.6008 0.6380 0.0111 0.1152 0.0771 0.6003 0.9861 0.0473 0.2355 0.3360 0.8173 0.3133 0.6045 0.6821 0.1103 0.1930 0.3148 0.6608 0.1082 0.4856 0.1991 0.7621 0.9948 0.4198 0.7858 0.9622 0.5720 0.5160 0.8098 0.6810 0.4578 0.5295 0.0804
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[Rsort,indsort] = sort(R);
indsort
indsort = 10x10
8 8 4 3 4 5 5 5 8 7 5 9 2 9 8 9 2 9 1 10 3 1 9 1 2 8 4 2 3 3 6 2 5 6 1 3 7 10 2 2 10 6 6 4 7 6 8 6 10 6 2 4 8 10 5 4 1 4 4 1 9 3 7 2 3 7 9 7 7 5 1 10 1 5 10 10 10 1 5 4 7 7 10 7 9 2 6 8 9 8 4 5 3 8 6 1 3 3 6 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
So, when you do the the
Rsorted = R(indsort)
Rsorted = 10x10
0.1152 0.1152 0.9472 0.3368 0.9472 0.3230 0.3230 0.3230 0.1152 0.8786 0.3230 0.6821 0.4524 0.6821 0.1152 0.6821 0.4524 0.6821 0.8288 0.4198 0.3368 0.8288 0.6821 0.8288 0.4524 0.1152 0.9472 0.4524 0.3368 0.3368 0.3826 0.4524 0.3230 0.3826 0.8288 0.3368 0.8786 0.4198 0.4524 0.4524 0.4198 0.3826 0.3826 0.9472 0.8786 0.3826 0.1152 0.3826 0.4198 0.3826 0.4524 0.9472 0.1152 0.4198 0.3230 0.9472 0.8288 0.9472 0.9472 0.8288 0.6821 0.3368 0.8786 0.4524 0.3368 0.8786 0.6821 0.8786 0.8786 0.3230 0.8288 0.4198 0.8288 0.3230 0.4198 0.4198 0.4198 0.8288 0.3230 0.9472 0.8786 0.8786 0.4198 0.8786 0.6821 0.4524 0.3826 0.1152 0.6821 0.1152 0.9472 0.3230 0.3368 0.1152 0.3826 0.8288 0.3368 0.3368 0.3826 0.6821
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
because of linear indexing
% Rsorted(1, 1) will be R(8)
% Rsorted(2, 1) will be R(5)
% Rsorted(8, 8) will be R(1)
Even though it's a matrix, you will see how to use one index to access the element in the link above.
As you see, your sort operated on columns, and it gave sorted indices for each column. You can fix this by adding the number of elements from the columns to the left which is second to last line
R = rand(10, 10);
[Rsort,indsort] = sort(R);
vec = 0 : height(R) : (height(R) * width(R) - 1);
indLinExp = indsort + vec; % care, linear expansion in MATLAB
Rsort - R(indLinExp)
ans = 10x10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!