how to find value in the matrix using index?

조회 수: 33 (최근 30일)
Chang seok Ma
Chang seok Ma 2021년 11월 16일
답변: KSSV 2021년 11월 16일
Hello,
I am wondering if I can find value using index.
For example, given matrix A, I would like to get third value of the first row and the second value from the second row.
I want to get 30 from the first row and 50 from the second row.
A = [10 20 30 ; 40 50 60];
B = [3;2]
Is there any way I could do this?
Thank you

채택된 답변

KSSV
KSSV 2021년 11월 16일
Read about MATLAB matrix indexing. If A is a matrix, you can extract (i,j) element using A(i,j).
Also have a look on ind2sub and sub2ind. As you know sub indices here, convert them into global indices using sub2ind and then extract.
A = [10 20 30 ; 40 50 60];
B = [3;2] ;
idx = sub2ind(size(A),[1;2],B)
idx = 2×1
5 4
A(idx)
ans = 2×1
30 50

추가 답변 (1개)

Cris LaPierre
Cris LaPierre 2021년 11월 16일
See Ch 5 of MATLAB Onramp.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by