Search rows of a matrix on another and read data respectively.

조회 수: 2 (최근 30일)
Mani Ahmadian
Mani Ahmadian 2014년 9월 27일
편집: dpb 2014년 9월 28일
Hi
I have a data set file with headers as 'LatDegree, LongDegree, LatUTM, LongUTM, Height'. I have a calculated matrix consists of just two headers as 'LatDegree, LongDegree', too. I attached a sample of these files.
I want to search each row of calculated matrix in data set and add other corresponded items to calculated matrix based on my data set.
How I should do that? I have to use vectorized code to improve the speed.
Thanks
Mani

답변 (1개)

dpb
dpb 2014년 9월 27일
편집: dpb 2014년 9월 28일
[~,ia]=intersect(A(:,1:2),B,'rows','stable'); % locations
B=[B A(ia,3:end)];
ADDENDUM
OK, I didn't look at the actual values; presumed there wouldn't be duplicates. To handle repeats in the lookup set, use ismember instead...
[~,ia]=ismember(B,A(:,[1:2]),'rows');
B=A(ia,:);
I often wish there were some way to write syntax to get the alternate return value w/o the temporary indexing vector...
  댓글 수: 2
Mani Ahmadian
Mani Ahmadian 2014년 9월 28일
Dear dpb
I examined your idea. It's not a perfect answer for my question. As you can try it, Data set is a 10*5 matrix and CalculatedMatrix is a 17*2.
I need to have a 17*5 matrix as my final result, but it returns me an error message: 'Dimensions of matrices being concatenated are not consistent.'
Note: some rows in CalculatedMatrix are repeated and I need to complete it with other colomns from Data Set.
Thanks a lot
Mani
dpb
dpb 2014년 9월 28일
편집: dpb 2014년 9월 28일
See ADDENDUM in Answer section -- as noted, didn't expect there to be repeats.
But, would have thought that seeing ismember would have caused to look at the doc to see if it could be made to handle the repeated fields and then the "See Also" cast of characters thereat would have led you to...
Just a side note on how to think about and approach solving these problems in Matlab on your own rather than waiting around until somebody gets back here...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by