Specifying values in 2D array when I know the indices

1 visualizzazione (ultimi 30 giorni)
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];

Risposta accettata

Fangjun Jiang
Fangjun Jiang il 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

Più risposte (0)

Categorie

Scopri di più su Programming Utilities in Help Center e File Exchange

Tag

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by