Indexing problem with matrices

2 ビュー (過去 30 日間)
Francesco Cattaneo
Francesco Cattaneo 2020 年 8 月 4 日
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

採用された回答

Bruno Luong
Bruno Luong 2020 年 8 月 4 日
編集済み: Bruno Luong 2020 年 8 月 4 日
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 件のコメント
Francesco Cattaneo
Francesco Cattaneo 2020 年 8 月 7 日
Thank you! It helped

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by