Specifying values in 2D array when I know the indices

1 vue (au cours des 30 derniers jours)
MichailM
MichailM le 30 Nov 2022
Commenté : MichailM le 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];

Réponse acceptée

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

Plus de réponses (0)

Catégories

En savoir plus sur Programming Utilities dans Help Center et File Exchange

Tags

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by