# Specifying values in 2D array when I know the indices

1 view (last 30 days)
MichailM on 30 Nov 2022
Commented: MichailM on 30 Nov 2022
I have the short example below. With the max function I find the indices of the maximum values whithin each column of array A. I want an array C which will include the values of array B by using the idx values I specified before. So, C will be C = [1 2 3 4 5]. How can I do that without a for-loop?
A = [1 1 1 1 100;
100 1 1 1 1
1 1 100 1 1
1 1 1 100 1;
1 100 1 1 1];
[~,idx] = max(A,[],1);
B = [1 1 1 1 5;
1 1 1 1 1
1 1 3 1 1
1 1 1 4 1;
1 2 1 1 1];

Fangjun Jiang on 30 Nov 2022
A = [1 1 1 1 100;
100 1 1 1 1
1 1 100 1 1
1 1 1 100 1;
1 100 1 1 1];
[~,idx] = max(A,[],1,'linear')
idx = 1×5
2 10 13 19 21
B = [1 1 1 1 5;
1 1 1 1 1
1 1 3 1 1
1 1 1 4 1;
1 2 1 1 1];
C=B(idx)
C = 1×5
1 2 3 4 5
MichailM on 30 Nov 2022
Thanks!

### Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by