Filter löschen
Filter löschen

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

17 Ansichten (letzte 30 Tage)
Ziyu Wang
Ziyu Wang am 30 Jan. 2018
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?

Antworten (1)

Harish Ramachandran
Harish Ramachandran am 2 Feb. 2018
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.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by