필터 지우기
필터 지우기

Matrix Indexig or how to access values of a matrix

조회 수: 2 (최근 30일)
Philipp ter Schiphorst
Philipp ter Schiphorst 2020년 5월 5일
댓글: Philipp ter Schiphorst 2020년 5월 6일
Hallo dear forum,
I just don't find a problem for my actually simple problem.
I have the following matrix:
A = magic(4)
I now want the values ​​of the matrix in position:
value1 = A(1,1)
value2 = A(2,2)
value3 = A(3,1)
My actual matrix indexes are in a vector in this form:
index = [1 1;2 2;3 1];
My solution (that is obviously wrong) would be:
solution = A(index)
Is there an easy way without using loops?
Thank you so much!

채택된 답변

Tommy
Tommy 2020년 5월 5일
You could convert to linear indices:
solution = A(sub2ind(size(A),index(:,1),index(:,2)));
  댓글 수: 1
Philipp ter Schiphorst
Philipp ter Schiphorst 2020년 5월 6일
Thank you very much, that was exactly what I was looking for

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by