Index a Y = MxN, with X=Mx5 (where elements in X are column IDs for values to extract from Y)

1 view (last 30 days)
Having a Y = MxN matrix of data, and a X = Mx5 (or other fixed nr. < than N) matrix obtained as:
[~, X] = maxk(Z,5,2); % where Z has the same size as Y
is there a non-FOR way to extract the values from Y coresponding to positions as expressed in X? Current solution is:
for line = 1:size(Y,1)
X1(line,:) = Y(line, X(line,:))
end

Accepted Answer

Voss
Voss on 6 Dec 2023
Edited: Voss on 6 Dec 2023
You can use sub2ind for that.
Example:
Y = randi(50,6,10)
Y = 6×10
16 15 5 1 3 27 32 25 19 34 48 32 21 13 39 24 10 10 21 43 19 42 38 40 24 37 32 1 2 23 41 41 13 26 46 24 1 31 9 48 4 39 17 29 4 43 5 27 42 38 5 17 18 9 14 29 41 23 32 49
Z = Y/100;
[~,X] = maxk(Z,5,2)
X = 6×5
10 7 6 8 9 1 10 5 2 6 2 4 3 6 7 10 5 1 2 8 6 9 2 10 4 10 7 9 6 8
% non for-loop method:
[M,N] = size(Y);
rows = repmat((1:M).',1,size(X,2));
cols = X;
X1 = Y(sub2ind([M N],rows,cols))
X1 = 6×5
34 32 27 25 19 48 43 39 32 24 42 40 38 37 32 48 46 41 41 31 43 42 39 38 29 49 41 32 29 23
% for-loop method:
X1_for = zeros(size(Y,1),size(X,2));
for line = 1:size(Y,1)
X1_for(line,:) = Y(line, X(line,:));
end
X1_for
X1_for = 6×5
34 32 27 25 19 48 43 39 32 24 42 40 38 37 32 48 46 41 41 31 43 42 39 38 29 49 41 32 29 23
% both methods produce the same result:
isequal(X1,X1_for)
ans = logical
1
  2 Comments
Andy
Andy on 6 Dec 2023
Edited: Andy on 6 Dec 2023
yes, sub2ind & repmat do show up in other links ... and it's a tad better than a for - still, not a one liner ... surprised there isn't one word function for this
Voss
Voss on 6 Dec 2023
Edited: Voss on 6 Dec 2023
Y = randi(50,6,10)
Y = 6×10
43 10 39 13 47 41 21 29 3 45 12 33 12 18 49 22 38 7 47 31 46 13 14 13 27 23 8 2 22 17 12 33 35 15 7 17 42 32 33 1 19 2 44 46 32 18 7 32 19 5 5 9 25 25 35 10 2 42 32 24
Z = Y/100;
[~,X] = maxk(Z,5,2)
X = 6×5
5 10 1 6 3 5 9 7 2 10 1 5 6 9 10 7 3 2 9 8 4 3 5 8 1 8 5 9 3 4
% one-liner:
X1 = Y((X-1)*size(Y,1)+(1:size(Y,1)).')
X1 = 6×5
47 45 43 41 39 49 47 38 33 31 46 27 23 22 17 42 35 33 33 32 46 44 32 32 19 42 35 32 25 25
% for-loop method:
X1_for = zeros(size(Y,1),size(X,2));
for line = 1:size(Y,1)
X1_for(line,:) = Y(line, X(line,:));
end
X1_for
X1_for = 6×5
47 45 43 41 39 49 47 38 33 31 46 27 23 22 17 42 35 33 33 32 46 44 32 32 19 42 35 32 25 25
% both methods produce the same result:
isequal(X1,X1_for)
ans = logical
1

Sign in to comment.

More Answers (0)

Categories

Find more on Just for fun in Help Center and File Exchange

Tags

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!