Search rows of a matrix on another and read data respectively.
2 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
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
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...
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!