What Is Vector Trap?

What is the most efficient way to test whether a matrix contains a given row vector in MatLab?

  • More specifically, that a given 1 by n row vector exists as a row in an m by n matrix, such that all elements are in the same order and are equal. The ismember() function is very slow, and my own attempt is better but still quite slow: function index = findIndexOfVectorInMatrix( matrix, vector ) % Finds the index of 'vector' as it appears in 'matrix', such that % >> index = findIndexOfVectorInMatrix( matrix, vector ); % >> matrix( index(1), : ) % returns the original 'vector'. % 'index' may itself be a vector of indices if there are % multiple copies of 'vector' in 'matrix'. [r,c]=size(vector); if r ~= 1 && c ~= 1 error('Vector must have one dimension of size = 1.'); end functionalString = 'find( sum( '; if r == 1 % Rows of matrix are vectors. functionalString = strcat( functionalString, [' ( matrix( :, 1 ) == vector( 1 ) )'] ); for ii = 2:c functionalString = strcat( functionalString, ... [' + ( matrix( :, ', num2str(ii),' ) == vector( ', num2str(ii),' ) )'] ); end functionalString = strcat( functionalString, ', 2 ) == c );' ); elseif c == 1 % Columns of matrix are vectors. functionalString = strcat( functionalString, [' ( matrix( 1, : ) == vector( 1 ) )'] ); for ii = 2:r functionalString = strcat( functionalString, ... [' + ( matrix( ', num2str(ii),', : ) == vector( ', num2str(ii),' ) )'] ); end functionalString = strcat( functionalString, ', 1 ) == r );' ); end [index] = eval( functionalString );

  • Answer:

    First post so I'm not sure if this will show up with the correct formatting but how about something like getting the absolute value of subtracting repmat(row vector,n,1) from your matrix, then getting the norm of each row vector and reporting if any norm =0. I tested the elapsed times of calculation for a magic 2000x2000 matrix searching for the last row using the following methods: 1. [~,index]=ismember(matrix, vector, 'rows') Elapsed time is 2.892619 seconds. 2. for i=1:numrows if ismember(matrix(i,:),vector) disp(i) end end Elapsed time is 1.060776 seconds. 3. absdiff=abs(matrix-repmat(row, numrows,1)); for i=1:numrows if norm(absdiff(i,:))==0 disp(i) end end Elapsed time is 0.363086 seconds.

Shawn Lyo at Quora Visit the source

Was this solution helpful to you?

Other answers

Some interesting results comparing my solution with 's. The best solution depends upon the input size and shape. For a matrix with many rows, each of which has a short length (as in my problem domain), my solution is the best by far: qq=rand(20000,5);qq(123,:)=qq(234,:);ww=qq(234,:); Test #1: Index(es): 123 234, found in 0.017106s. Test #2: Index(es): 123 234, found in 1.3086s. Test #3: Index(es): 123 234, found in 0.021746s. Test #4: Index(es): 123 234, found in 0.0041062s. However, for a matrix with very long rows, my solution is terrible, and 's is better: qq=rand(2000);qq(123,:)=qq(234,:);ww=qq(234,:); Test #1: Index(es): 123 234, found in 0.71609s. Test #2: Index(es): 123 234, found in 0.4104s. Test #3: Index(es): 123 234, found in 0.13792s. Test #4: Index(es): 123 234, found in 2.2145s. The full benchmark script is below if you would like to verify these results for yourself. (Test #4 uses the findIndexOfVectorInMatrix function in the question details.) test=1; tic; [~,indexes]=ismember(qq, ww, 'rows'); indexes=find(indexes==1)'; disp(['Test #',num2str(test),': Index(es): ',num2str(indexes),', found in ',num2str(toc),'s.']) test=2; tic; indexes = []; for i=1:size(qq,1) if ismember(qq(i,:),ww) indexes=[indexes,i]; end end disp(['Test #',num2str(test),': Index(es): ',num2str(indexes),', found in ',num2str(toc),'s.']) test=3; tic; indexes = []; absdiff=abs(qq-repmat(ww, size(qq,1),1)); for i=1:size(qq,1) if norm(absdiff(i,:))==0 indexes=[indexes,i]; end end disp(['Test #',num2str(test),': Index(es): ',num2str(indexes),', found in ',num2str(toc),'s.']) test=4; tic; indexes = findIndexOfVectorInMatrix( qq, ww )'; disp(['Test #',num2str(test),': Index(es): ',num2str(indexes),', found in ',num2str(toc),'s.'])

Anonymous

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.