How to retrieve specific rows that the coordinates x,y have been saved in another matrix?

3 ビュー (過去 30 日間)
Sara Salimi
Sara Salimi 2016 年 11 月 1 日
コメント済み: Guillaume 2016 年 11 月 1 日
Hi,
I have a data matrix Data(8765x138) that first and second columns are x and y coordinates. I have sampled some specific points in another array, Points(2000x2), first and second columns in A refers to x and y, respectively. I want to extract some specific rows in Matlab that match with matrix A (both x,y). The output should be (2000x138). I tried the following code but the result is not correct.
newData = Data(ismember(Data(:,1),Points(:,1))& ismember(Data(:,2),Points(:,2)),:);
what should I do to select the rows from Data that its first and second columns match to my Points matrix. Someone please help, I feel like I've tried everything!

回答 (1 件)

Guillaume
Guillaume 2016 年 11 月 1 日
The problem with your statement is that it finds all rows of Data whose x match an x in Points and match any y in Points but not necessarily in the same Points row. To fix this, you need to use the 'row' option of ismember:
newData = Data(ismember(Data(:, [1 2]), Points(:, [1 2]), 'rows'), :)
  2 件のコメント
Sara Salimi
Sara Salimi 2016 年 11 月 1 日
Dear Guillaume, Thank you very much. it works but why the number of rows in newData is not equal to the number of rows in Points Matrix?!
Guillaume
Guillaume 2016 年 11 月 1 日
I see now reasons why the two should be equal unless each row of Points is present and present only once in Data.

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

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by