简体   繁体   中英

Finding the rows of a matrix with specified elements

I want to find the rows of a matrix which contain specified element of another matrix.

For example, a=[1 2 3 4 5 6 7] and b=[1 2 0 4;0 9 10 11;3 1 2 12] . Now, I want to find the rows of b which contain at least three element of a . For this purpose, I used bsxfun command as following:

c=find(sum(any(bsxfun(@eq, b, reshape(a,1,1,[])), 2), 3)>=3);

It works good for low dimension matrices but when I want to use this for high dimension matrices, for example, when the number of rows of b is 192799 , MATLAB gives following error:

Requested 192799x4x48854 (35.1GB) array exceeds maximum array size preference. 
Creation of arrays greater than this limit may take a long time and cause MATLAB 
to become unresponsive. See array size limit or preference panel for more information.

Is there any other command which does this task without producing the behaviour like above for high dimension matrices?

a possible solution:

a=[1 2 3 4 5 6 7] 
b=[1 2 0 4;0 9 10 11;3 1 2 12]
i=ismember(b,a)
idx = sum(i,2)
idx = find(idx>=3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM