Borrar filtros
Borrar filtros

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

17 visualizaciones (últimos 30 días)
Ziyu Wang
Ziyu Wang el 30 de En. de 2018
Respondida: Harish Ramachandran el 2 de Feb. de 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?

Respuestas (1)

Harish Ramachandran
Harish Ramachandran el 2 de Feb. de 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.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by