calling corresponding elements of two matrices

2 visualizzazioni (ultimi 30 giorni)
Hello,
I have a matrix A as shown below:
A = [1, 2, 3; -1 -5, -3; 0, -2, 4]
I also have the matrix B=A.^2 (elementwise squaring)
So I expect
B = [1 , 4, 9; 1, 25, 9; 0 , 4, 16]
I want to get the maximum element PER ROW of B, so
[M,I] = max(B, [], 2)
M will be a column matrix containing the maximum of each row, and I will be the positions/locations of these maximum.
I = [3, 2, 3]
Now, I want to use matrix I to refer to the counterparts in A. Meaning, I want to call the elements 3, -5, 4 and make them a column matrix. How should I do this?
Thanks

Risposta accettata

Azzi Abdelmalek
Azzi Abdelmalek il 1 Dic 2014
Modificato: Azzi Abdelmalek il 1 Dic 2014
A = [1, 2, 3; -1 -5, -3; 0, -2, 4]
B=A.^2
[M,ii] = max(B, [], 2)
out=A(sub2ind(size(A),1:size(ii,1),ii'))

Più risposte (0)

Categorie

Scopri di più su Data Types in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by