Finding If a sub-array (1d) is there in a 2d array

10 ビュー (過去 30 日間)
musi
musi 2011 年 6 月 12 日
Hi,
Lets suppose we have
array2D = [1 2 3; 4 5 6; 6 7 8] how to find out cheaply that it contain "4 5 6" in a position?
I have a main dataset, which is like 10M samples and I have a test set conatining like 2M samples. The test set is a sub set of main set (infact randomly selected elements of main array). I need to find the index of each test sample into the main data file.
Pls give me a solution, as I am not able to do it cheaply yet.
Thanks in advance

採用された回答

Paulo Silva
Paulo Silva 2011 年 6 月 12 日
For the example provided
array2D = [1 2 3; 4 5 6; 6 7 8]
v1= [4 5 6]
[tf,loc]=intersect(array2D,v1,'rows')
loc gives you the row number where you have v1 inside array2D, you might also take a look at the ismember function for similar purposes
  2 件のコメント
musi
musi 2011 年 6 月 12 日
Many thanks, it is v good.
Just one comment, I only need indexes, which methid wld be fast?
I think is member can solve problem for all test samples in one go, and might be fast.
cheer
Paulo Silva
Paulo Silva 2011 年 6 月 12 日
after you get the row number you can get all the index values, this is just for your example:
as=size(array2D)
idx=loc:as(1):numel(array2D) %all the index values
The code only works if there's just one row equal to v, if there are more use arrayfun

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

その他の回答 (0 件)

カテゴリ

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