Indexing problem with matrices

2 views (last 30 days)
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

Accepted Answer

Bruno Luong
Bruno Luong on 4 Aug 2020
Edited: Bruno Luong on 4 Aug 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

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!