calling corresponding elements of two matrices

1 次查看(过去 30 天)
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

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2014-12-1
编辑:Azzi Abdelmalek 2014-12-1
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'))

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by