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

2 次查看(过去 30 天)
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