4.0

4.0 | 3 ratings Rate this file 94 downloads (last 30 days) File Size: 23.15 KB File ID: #9095

Binary Serach of rows of M-by-N Matrix

by Dr. Murtaza Khan

 

19 Nov 2005 (Updated 09 Jul 2009)

Code covered by BSD License  

Binary Search that is based on sum of column values of rows of input M-by-N Matrix

Download Now | Watch this File

File Information
Description

Binary Serach of rows of M-by-N Matrix
Search is based on sum of column values of rows of input M-by-N Matrix
i.e. Rows of input Matrix must already be sorted in ascending order
based on sum of their column values. i.e.
row1sum<row2sum<...rowjsum<...rowmsum
where rowjsum=sum(mat(j,:),2) (i.e sum of column values)

If mat is not sorted use following two statments to sort rows of mat
according to sum of column values
matwithsum=sortrows([sum(mat,2),mat],1);
mat=matwithsum(:,2:end);

INPUT:
mat: Matrix contains values such as
     [x1, y1,..., zn;
     x2, y2,..., zn;
     ...
     xm, ym,..., zn];

srow: row that is to be searched in mat

OUTPUT:
Using following different value of parameter 'type' following output can be return
1. 'first': returns 'indvec1' i.e index of first occured row in mat that matches to srow.

2. 'all': returns 'indvec1' i.e indices of all rows in mat that matches to srow.

3. 'last': returns 'indvec1' i.e index of last row in mat that matches to srow.

4. 'allcolsum': returns 'indvec1' i.e indices of all k rows of mat such that
sum(mat(j,:),2)=sum(srow,2),
where 1<=j<=k

5. 'colsumandval': return two index vector first is similar to return by "allcolsum" and second is similiar to return by "all' option.

Note that if we are searching e.g. [5 4 2] by option 'first' then there may be many rows whose column sum is equal to given row e.g. [2 4 5],[0 0 11],[4 2 5], [5 4 2] but only
last row i.e. [5,4,2] matches in all columns to given row to be searched.
-------------------------
bsearchmatrows.m (main program)
bsearchmatrowsTest1.m (simple test program)
bsearchmatrowsTest2.m (compare performance with MATLAB builtin metod index=find(ismember(mat,srow, 'rows'));
-------------------
For large data this search is extremely faster than MATLAB built in methods

MATLAB release MATLAB 6.5 (R13)
Zip File Content  
Other Files bsearchmat/bsearchmatrows.GIF,
bsearchmat/bsearchmatrows.m,
bsearchmat/bsearchmatrowsTest1.m,
bsearchmat/bsearchmatrowsTest2.m,
bsearchmat/findindofeqcolsumrows.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 (5)
22 Nov 2005 Jos van der Geest

This is not faster than the proper use of ismember(M,srow,'rows').

23 Nov 2005 M A Khan

in reply to Jos van der Geest
-----------------------------
if you are seraching small data set (e.g. matrix of few hundrend rows) then you will not feel the difference. But for large data set in thoudsands of rows then this is definitley faster then MATLAB builtin method "find(ismember(mat,srow, 'rows'))". It is easy to understand, MATLAB ismember method first sort the data, the sorting complexity of fastest algorithm merge sort/quick sort is O(nlogn) then ismember serach the data. Serach complexity of binary serach is O(logn) therefore if you use MATLAB builtin method the overall seraching complexity would be O(nlogn)+O(logn)=O(nlogn). If the data is already sorted then there is not need to sort it again, but if you use MATLAB buitin method it spend time to sort. But if you use the "bsearchmatrows" it will not sort the data , and searching complexity would be limit to O(logn). I have tested this program tens of times (see bsearchmatrowsTest2.m ),it always faster then MATLAB builtin methods.
-----------------------------
check again the program bsearchmatrowsTest2.m (as the data size grow , differece of peroframance goes in favour of bsearchmatrows.m)

30 Nov 2007 Amfy J.

Very useful for searching rows in large 2-D matrices.

12 Jun 2009 woonsang cho

It works for small matrices, but it does not work for some larger matrices. Please run and check the following statement:

bsearchmatrows([5 6; 2 3 ; 1 2 ; 1 1; 0 0],[5 6])

ans =
     []

Obviously it should return '1', but it doesn't. Please help us fix this bug. Thank you,

12 Jun 2009 woonsang cho

sorry I realized that it should be ordered by the sum. thank you

Please login to add a comment or rating.
Updates
09 Jul 2009

BSD License

Tag Activity for this File
Tag Applied By Date/Time
binary serach Dr. Murtaza Khan 22 Oct 2008 08:07:09
matrices Dr. Murtaza Khan 22 Oct 2008 08:07:09
search Dr. Murtaza Khan 22 Oct 2008 08:07:09
find Dr. Murtaza Khan 22 Oct 2008 08:07:09
ismember Dr. Murtaza Khan 22 Oct 2008 08:07:09
 

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