calling corresponding elements of two matrices

1 view (last 30 days)
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

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 1 Dec 2014
Edited: Azzi Abdelmalek on 1 Dec 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'))

More Answers (0)

Categories

Find more on Data Types in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!