Binary Serach of rows of M-by-N Matrix
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
Cite As
Dr. Murtaza Ali Khan (2024). Binary Serach of rows of M-by-N Matrix (https://www.mathworks.com/matlabcentral/fileexchange/9095-binary-serach-of-rows-of-m-by-n-matrix), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxCategories
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.