3.66667

3.7 | 4 ratings Rate this file 2 Downloads (last 30 days) File Size: 23.15 KB File ID: #9095
image thumbnail

Binary Serach of rows of M-by-N Matrix

by Dr. Murtaza Khan

 

19 Nov 2005 (Updated 09 Jul 2009)

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

| 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)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (6)
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

11 Mar 2010 Thomas Trötscher

This does not work well when there are many rows with the same column sum. I have a matrix of size ~45000 x 350 where the rows can have a sum of 1, 2 or 3; in this case the supplied function used about 100 times as long as the built-in matlab function. Also, if the same data is transposed the function does not work at all.

Other than that it seems to work very well for uniformly distributed data.

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

Contact us at files@mathworks.com