Indexing problem with matrices

2 vues (au cours des 30 derniers jours)
Francesco Cattaneo
Francesco Cattaneo le 4 Août 2020
Hi everyone, I've got a problem, I don't know if there's an easy solution.
I have two matrices:
A = [3 5 9 6 ; 2 1 13 6 ; 6 8 52 4 ; 13 6 8 22 ];
GRID = [linspace(1,100,100); rand(1,100); rand(1,100)]';
Each element of A is the index of the point of a structure. GRID sorts every point of the structure and associates the x and y coordinate in the 2nd and 3rd column.
The aim is to create a matrix Ax in which actually every element is the x coordinate of the correspondant A element (taken from the 2nd colum of GRID) and a a matrix Ay in which every element is the y coordinate of the correspondant A element (taken from the 3rd colum of GRID).
I cant use a for because A is very big and it takes too long. I dont know how to do.
Thanks to everyone

Réponse acceptée

Bruno Luong
Bruno Luong le 4 Août 2020
Modifié(e) : Bruno Luong le 4 Août 2020
A = [3 5 9 6 ; 2 1 13 6 ; 6 8 52 4 ; 13 6 8 22 ];
GRID = [linspace(1,100,100); rand(1,100); rand(1,100)]'
Code
[tf,loc]=ismember(A,GRID(:,1)); % or possible loc = A, if it's already index
Ax=nan(size(loc)); Ax(tf)=GRID(loc(tf),2);
Ay=nan(size(loc)); Ay(tf)=GRID(loc(tf),3);
Result
Ax
Ay
  1 commentaire
Francesco Cattaneo
Francesco Cattaneo le 7 Août 2020
Thank you! It helped

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

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

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by