View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

5.0 | 8 ratings Rate this file 18 Downloads (last 30 days) File Size: 3.73 KB File ID: #23998 Version: 1.2
image thumbnail



Loginatorist (view profile)


04 May 2009 (Updated )

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

| Watch this File

File Information

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)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (11)
15 Sep 2016 ty666

ty666 (view profile)


13 Feb 2015 Kotya Karapetyan

09 Jan 2015 Jeremiah

Excellent code!

12 Nov 2013 James

James (view profile)

11 Mar 2010 Thomas Trötscher

20 Jul 2009 Karthik Mahadevan

02 Jun 2009 Loginatorist

Loginatorist (view profile)

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.

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

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

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.

Comment only
02 Jun 2009 Siyi Deng

Siyi Deng (view profile)

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));

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

Comment only
06 May 2009 us

us (view profile)

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

05 May 2009 Loginatorist

Loginatorist (view profile)

Good point us, I will submit an update.

Comment only
05 May 2009 us

us (view profile)

- 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)...

06 May 2009 1.1

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

17 Jun 2009 1.2

Faster engine for larger B matrices.

Contact us