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

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

Answers (1)

dpb
dpb on 27 Sep 2014
Edited: dpb on 28 Sep 2014
[~,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 Comments
Mani Ahmadian
Mani Ahmadian on 28 Sep 2014
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 on 28 Sep 2014
Edited: dpb on 28 Sep 2014
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...

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!