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

49 ビュー (過去 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 Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by