Code covered by the BSD License  

Highlights from
mfind

3.66667

3.7 | 3 ratings Rate this file 9 Downloads (last 30 days) File Size: 1.35 KB File ID: #30875

mfind

by Marcello Ferro

 

29 Mar 2011 (Updated 30 Mar 2011)

Find matching rows or columns in a matrix

| Watch this File

File Information
Description

MFIND Find matching rows or columns in a matrix

   [ I B ] = mfind(M, X)

   Find the row or column vector X within the matrix M.
   If X is a row vector, I will contain the column indexes where X is a row in M.
   If X is a column vector, I will contain the row indexes where X is a column in M.
   B will contain the boolean matching rows or columns.
   If no match if found, I will be empty.

   Example:
      a = [1 2; 3 4; 5 6];

      [i b] = mfind(a, [3 4])
      i = 2
      b = [ 0; 1; 0 ]

      [i b] = mfind(a, [1; 3; 5])
      i = 1
      b = [ 1 0 ]

MATLAB release MATLAB 7.9 (2009b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
31 Mar 2011 John D'Errico

A good tool, that does what it claims to do. It uses a good algorithm for the solution. It has quite clear help, with an H1 line to describe & find the function.

The only thing I would have done beyond this is add a test or so to verify the inputs. For example, what happens in this code if X is an array? In fact, if the array has the same number of columns as M, then it runs without producing an error, but perhaps the result is a surprise to the user. This behavior is definitely undocumented.

I would suggest adding a test to verify that X is a vector. If it passes that first test, then it must also conform in size with M, so a row vector must have the same number of columns as M. If it is a column vector, then it must have the same number of rows as M.

Failing these tests should return an error message that will help the user to see what they have done wrong. The best code is friendly code, that fails softly. Don't let your code return a confusing error message from ismember, or a completely unexpected result they will then have to track down as a bug in their own code.

Well written in all other respects though.

03 Nov 2011 Justin Winokur  
03 Nov 2011 Justin Winokur

I originally wrote the search with nested for loops (I know, I should be shot), but then to "optimize" it, I used ismember and find to make it better. Unfortunately, it slowed it down rather significantly. I was excited to see this code, but it turn out that it does it the same way. Too bad.

11 May 2012 Ioannis Filippidis

Handy code, thanks!

Please note that using the conjugate transpose operator ' can cause issues in case complex numbers are involved.

Instead, the non-conjugate transpose .' flips the matrix without affecting the numbers.

Please login to add a comment or rating.
Updates
30 Mar 2011

v1.0.1 Minor corrections in help section
v1.0.0 First release

Tag Activity for this File
Tag Applied By Date/Time
find Marcello Ferro 29 Mar 2011 14:58:21
matching rows Marcello Ferro 29 Mar 2011 14:58:21
matching columns Marcello Ferro 29 Mar 2011 14:58:21

Contact us at files@mathworks.com