Mapping indices of matrices with differing dimension

1 vue (au cours des 30 derniers jours)
Joshua Goglio
Joshua Goglio le 18 Juil 2018
Modifié(e) : Guillaume le 18 Juil 2018
This seems simple, but I cannot find an answer to my specific question. Take the matrices below:
x = [5 3 7 0 1];
y = [1 3 3 1; 2 2 2 2; 3 5 5 3; 1 3 3 1];
I'd like to create a new matrix 'z' that takes each element of 'y' and pulls the value of 'x' using the 'y' value as an index of 'x':
z = [5 7 7 5; 3 3 3 3; 7 1 1 7; 5 7 7 5];
Kind of like x(y) but for matrices of differing dimension.
My real question is whether this can be done without a for loop, since I'm new to MATLAB and not as familiar with matrix manipulation as I could be.

Réponse acceptée

Guillaume
Guillaume le 18 Juil 2018
Modifié(e) : Guillaume le 18 Juil 2018
Well have you try x(y)? It will give you exactly your desired z.
Note that the rule of A(B) indexing is that it is the same shape as B unless both A and B are vectors (in which case, absurdly, it is the same shape as A).

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by