calling corresponding elements of two matrices

조회 수: 2 (최근 30일)
cgo
cgo 2014년 12월 1일
댓글: cgo 2014년 12월 1일
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에서 Data Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by