calling corresponding elements of two matrices

2 visualizaciones (últimos 30 días)
cgo
cgo el 1 de Dic. de 2014
Comentada: cgo el 1 de Dic. de 2014
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

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 1 de Dic. de 2014
Editada: Azzi Abdelmalek el 1 de Dic. de 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'))

Más respuestas (0)

Categorías

Más información sobre Data Types en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by