5.0

5.0 | 3 ratings Rate this file 130 downloads (last 30 days) File Size: 3.73 KB File ID: #23998

findsubmat

by Matt Fig

 

04 May 2009 (Updated 17 Jun 2009)

Code covered by BSD License  

Finds one matrix (a sub-matrix) within another.

Download Now | Watch this File

File Information
Description

FINDSUBMAT find one matrix (a sub-matrix) inside another.
IDX = FINDSUBMAT(A,B) looks for and returns the linear index of the
location of matrix B within matrix A. The index IDX corresponds to the location of the first element of matrix B within matrix A. Only works for 2-d matrices or vectors, both of which may contain NaNs or Infs.

[R,C] = FINDSUBMAT(A,B) returns the row and column instead.

I would like to extend to N-D but don't have the time right now. This may be a future enhancement, but I believe the file stands on it's own just fine too.

Email me if bugs are found, thanks.

MATLAB release MATLAB 7.4 (R2007a)
Zip File Content  
Other Files findsubmat.m,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (6)
05 May 2009 us

- very nice: help, example, commented and intelligent engine, acknowledgement of input by another user (excellent academic behavior)...
- BUT: unfortunately, it does NOT (yet) handle NANs - nor does it give a warning/error message...
- a workaround could be to replace NANs with a unique value not found in either data set (we did this in FEX:FPAT:id:6070)...
us

05 May 2009 Matt Fig

Good point us, I will submit an update.

06 May 2009 us

observing a master carving out an outstanding masterpiece of a master's workmanship...
us

02 Jun 2009 Siyi Deng

Matt: your code is well written but I really don't understand why use strfind to serch line by line. Compare it to these codes:

% Sorry I'm not used to use capital a,b;

idx = find(a(1:(ra*(ca-cb+1)-rb+1)) == b(1));
idx = idx(mod(idx-1,ra) <= ra-rb);

idc = bsxfun(@plus,0:ra:ra*(cb-1),(0:rb-1).');
c = 2;
d = b(c) == a(idx+idc(c));
while ~all(d)
    idx = idx(d);
    c = c+1;
    d = b(c) == a(idx+idc(c));
end

for general large matrices It seems that searching line by line usually results in overkill.

02 Jun 2009 Matt Fig

Siyi, Your code, as written, will work when there is one submatrix within A. However, it will need to be modified for multiple submatrices. Even without the modification, it is slower to use the approach you suggest. In the following, I made a copy of findsubmat, took out all of the NaN checking and the transposing of A, and put your code in the proper place. Here are some results:

A = round(rand(2000)*1);
B = [1 2;4 5];
A(30:31,60:61) = B; % Only one submatrix.

tic
C = findsubmat(A,B); % Elapsed time is 0.055974 seconds.
toc

tic
C = findsubmat_2(A,B); % Elapsed time is 0.502954 seconds.
toc

Also, this is really the kind of exchange that better takes place through email. Mine is listed in the help for findsubmat.
Feel free to email me about this code.
Thanks.

20 Jul 2009 Karthik Mahadevan  
Please login to add a comment or rating.
Updates
06 May 2009

Added ability to deal with NaNs, as suggested by Urs. S.

17 Jun 2009

Faster engine for larger B matrices.

Tag Activity for this File
Tag Applied By Date/Time
submatrix Matt Fig 05 May 2009 09:25:32
find Matt Fig 05 May 2009 09:25:33
inside Matt Fig 06 May 2009 09:21:01
vector Matt Fig 06 May 2009 09:21:01
matrix Matt Fig 06 May 2009 09:21:01
indexing Siyi Deng 02 Jun 2009 02:02:12
 

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