5.0

5.0 | 3 ratings Rate this file 150 downloads (last 30 days) File Size: 2.23 KB File ID: #14520

MATCHROW (v1.1, may 2008)

by Jos

 

04 Apr 2007 (Updated 14 May 2008)

Code covered by BSD License  

Match elements in the rows of a matrix

Editor's Notes:

This file was a File Exchange Pick of the Week

Download Now | Watch this File

File Information
Description

MATCHROW - match elements in the rows of a matrix

TF = MATCHROW(M,S) returns a column vector containing a logical 1 (true)where the rows of M contain (in any order) all the elements of S and 0 (false) otherwise. M and S can be a cell array of strings.
If a element occurs N times in S, than it has to be present at least N times in these rows of M as well.
 
Examples:
      M = [ 1 2 3 4 5 ; 1 2 2 4 3; 1 3 4 4 3 ; 3 2 4 2 5 ; 1 4 3 2 4 ] ;
      Q = matchrow(M,[1 2 4]) % -> [1 ; 1 ; 0 ; 0 ; 1] ;
      M(Q,:) % -> 1 2 3 4 5
             % 1 2 2 4 3
             % 1 4 3 2 4
 
      Q = matchrow(M,[1 2 4 4])
      M(Q,:) % -> 1 4 3 2 4
 
      Q = matchrow({'a','X','c','Y'; 'a','X','Y','d' ;
         'b','X','d','e'},{'X','Y'}) % -> [1 ; 1 ; 0] ;
 
      A = rand(999,999) ;
      x = 27 ;
      tic
      find(matchrow(A,A(x,:))) == x % -> true!
      toc
 
MATCHROW is vectorized and pretty quick ...

version 1.1, may 2008

MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
04 Apr 2007 C. S.

Useful, thanks.

04 Apr 2007 John D'Errico

Good, readable help. Error checks. Good examples, and an H1 line. Nicely vectorized. Its got a "see also" line, the author's name, date written, etc. There are many internal comments, a proper use of warning, and no mlint flags.

This is an example of a code other authors should emulate.

04 Sep 2009 Mauricio Romero  
Please login to add a comment or rating.
Updates
05 Apr 2007

fixed some minor spelling errors

14 May 2008

- fixed small error

Tag Activity for this File
Tag Applied By Date/Time
matrices Jos 22 Oct 2008 09:07:02
find row match ismember set vector search which Jos 22 Oct 2008 09:07:02
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com