필터 지우기
필터 지우기

How can I extract single value out of a matrix using vectors?

조회 수: 17 (최근 30일)
Ziyu Wang
Ziyu Wang 2018년 1월 30일
답변: Harish Ramachandran 2018년 2월 2일
Hello Matlab community,
For my program, I would like to use a vector as index to read value in a matrix. Given A, a matrix that can also have more than 2 dimensions and v, a col-vector. E.g.:
A = magic(4); v = [2;3];
n = A(2,3); % value I want to read
Assuming the format is correct, i.e. dim(A) = size(v,1) and the values in v do not go beyond the corresponding size in A and consist of only integers, how can I read n using only v?

답변 (1개)

Harish Ramachandran
Harish Ramachandran 2018년 2월 2일
A trivial implementation involves something along the lines of:
A = magic(4);
v = [2;3];
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
A(v(1),v(2))
ans =
10
You can program this sequence in order to automate element access for bigger problems.

카테고리

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