Effacer les filtres
Effacer les filtres

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

17 vues (au cours des 30 derniers jours)
Ziyu Wang
Ziyu Wang le 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?

Réponses (1)

Harish Ramachandran
Harish Ramachandran le 2 Fév 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.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by