# How to find the length of intersection of rows of a matrix?

2 views (last 30 days)
Prashant Bhagat on 19 Jul 2022
Commented: Voss on 20 Jul 2022
I have a matrix
A = 1 1 1 0 0 0 0 0 0 0
0 0 0 0 1 1 1 0 0 0
0 0 1 1 1 0 1 1 1 0
0 1 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 1 1 0 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 0 0
0 0 1 1 1 0 1 1 1 0
0 0 0 0 1 1 1 1 1 1
I want to find out the intersection between the row of the matrix and the output should be the no. of overlapping for each ones and lenth of overlapping.
For example, for row 1 and 3 column 3 has one length overlapping, row 1 and 6 have 3 length of overlapping, row 3 and 4 have two length overlapping. I wnat to do the same for all rows of the matrix.
Is there any way how we can figure out this?

Voss on 19 Jul 2022
A = [ 1 1 1 0 0 0 0 0 0 0
0 0 0 0 1 1 1 0 0 0
0 0 1 1 1 0 1 1 1 0
0 1 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 1 1 0 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 0 0
0 0 1 1 1 0 1 1 1 0
0 0 0 0 1 1 1 1 1 1 ];
row_pairs = nchoosek(1:size(A,1),2);
disp(row_pairs)
1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 3 4 3 5 3 6 3 7 3 8 3 9 3 10 4 5 4 6 4 7 4 8 4 9 4 10 5 6 5 7 5 8 5 9 5 10 6 7 6 8 6 9 6 10 7 8 7 9 7 10 8 9 8 10 9 10
n_pairs = size(row_pairs,1);
n_overlaps = zeros(n_pairs,1);
for ii = 1:n_pairs
n_overlaps(ii,1) = nnz(A(row_pairs(ii,1),:) & A(row_pairs(ii,2),:));
end
disp([row_pairs n_overlaps]);
1 2 0 1 3 1 1 4 2 1 5 0 1 6 3 1 7 0 1 8 1 1 9 1 1 10 0 2 3 2 2 4 0 2 5 0 2 6 3 2 7 0 2 8 3 2 9 2 2 10 3 3 4 2 3 5 0 3 6 3 3 7 0 3 8 5 3 9 6 3 10 4 4 5 0 4 6 2 4 7 0 4 8 2 4 9 2 4 10 0 5 6 0 5 7 0 5 8 0 5 9 0 5 10 0 6 7 0 6 8 4 6 9 3 6 10 3 7 8 0 7 9 0 7 10 0 8 9 5 8 10 4 9 10 4
Prashant Bhagat on 20 Jul 2022
Thanks. this works fine. I have one more thing to ask, how i can get the different length of intersection like how many intersection of length one, two and three and many more....?
Voss on 20 Jul 2022
This will give you a vector of overlap/intersection lengths for each pair of rows of A:
A = [ 1 1 1 0 0 0 0 0 0 0
0 0 0 0 1 1 1 0 0 0
0 0 1 1 1 0 1 1 1 0
0 1 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 1 1 0 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 0 0
0 0 1 1 1 0 1 1 1 0
0 0 0 0 1 1 1 1 1 1 ];
n_rows = size(A,1);
row_pairs = nchoosek(1:n_rows,2);
n_pairs = size(row_pairs,1);
overlap_lengths = cell(n_pairs,1);
for ii = 1:n_pairs
overlap = A(row_pairs(ii,1),:) & A(row_pairs(ii,2),:);
start_idx = strfind([false overlap],[false true]);
end_idx = strfind([overlap false],[true false]);
overlap_lengths{ii} = end_idx-start_idx+1;
end
result = array2table(row_pairs,'VariableNames',{'Row 1','Row 2'});
result.overlap_lengths = overlap_lengths;
disp(result)
Row 1 Row 2 overlap_lengths _____ _____ _______________ 1 2 {0×0 double} 1 3 {[ 1]} 1 4 {[ 2]} 1 5 {0×0 double} 1 6 {[ 3]} 1 7 {0×0 double} 1 8 {[ 1]} 1 9 {[ 1]} 1 10 {0×0 double} 2 3 {[ 1 1]} 2 4 {0×0 double} 2 5 {0×0 double} 2 6 {[ 3]} 2 7 {0×0 double} 2 8 {[ 3]} 2 9 {[ 1 1]} 2 10 {[ 3]} 3 4 {[ 2]} 3 5 {0×0 double} 3 6 {[ 1 1 1]} 3 7 {0×0 double} 3 8 {[ 3 2]} 3 9 {[ 3 3]} 3 10 {[ 1 3]} 4 5 {0×0 double} 4 6 {[ 2]} 4 7 {0×0 double} 4 8 {[ 2]} 4 9 {[ 2]} 4 10 {0×0 double} 5 6 {0×0 double} 5 7 {0×0 double} 5 8 {0×0 double} 5 9 {0×0 double} 5 10 {0×0 double} 6 7 {0×0 double} 6 8 {[ 1 3]} 6 9 {[ 1 1 1]} 6 10 {[ 3]} 7 8 {0×0 double} 7 9 {0×0 double} 7 10 {0×0 double} 8 9 {[ 3 2]} 8 10 {[ 4]} 9 10 {[ 1 3]}
To find how many overlaps/intersections are of each possible length, over all pairs of rows:
all_overlap_lengths = [overlap_lengths{:}];
max_overlap = size(A,2);
n_overlaps = zeros(1,max_overlap);
for ii = 1:max_overlap
n_overlaps(ii) = nnz(all_overlap_lengths == ii);
end
disp(n_overlaps);
16 7 12 1 0 0 0 0 0 0

Monica Roberts on 19 Jul 2022
You can use the "and" command to find which values of each vector both are "true" or equal to 1. If you sum up those values you will get the total number of "overlapping" as you say.
sum(and(A(1,:),A(6,:))) % How many 1's in both 1st and 6th row

vishweshwar samba on 19 Jul 2022
Edited: vishweshwar samba on 19 Jul 2022
Created all posssible row combinations found the overlaping and placed them in the table to compare
A = [ 1 1 1 0 0 0 0 0 0 0;
0 0 0 0 1 1 1 0 0 0;
0 0 1 1 1 0 1 1 1 0;
0 1 1 1 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0;
1 1 1 0 1 1 1 0 0 0;
0 0 0 0 0 0 0 0 0 0;
0 0 1 1 1 1 1 1 0 0;
0 0 1 1 1 0 1 1 1 0;
0 0 0 0 1 1 1 1 1 1 ];
% Create all possible row combinations
a1 = 1:numel(A(1,:));
a2 = 1:numel(A(:,1));
a3 = combvec(a1,a2)';
% Assign columns of a3 to R1 and R2 to make the code readable
R1 = a3(:,1);
R2 = a3(:,2);
% finding the overlapps and counting them
z = sum(and(A(R1,:),A(R2,:)),2);
% Table the data
overlapping = table(R1,R2,z)
overlapping = 100×3 table
R1 R2 z __ __ _ 1 1 3 2 1 0 3 1 1 4 1 2 5 1 0 6 1 3 7 1 0 8 1 1 9 1 1 10 1 0 1 2 0 2 2 3 3 2 2 4 2 0 5 2 0 6 2 3
##### 3 CommentsShow 1 older commentHide 1 older comment
vishweshwar samba on 19 Jul 2022
In the above code I have defined the matrix 'A' as mentioned in your question, after that I found all the possible combinations of rows. Then calculated the overlapping for all the combinations and placed them in the table. In the table R1 and R2 are rows and Z id the overlapping value. Check for this in the table - search for 1 in R1 and 3 in R2 and the value is z = 3 which is one length overlapping.
Best way to learn and understand the code is to copy the above code into MATLAB Live editor and then Debug it.
To learn about the functions that are used in the code, place your mouse pointer on the function and right click, then select third option "Help on xxx". Documentation related to the function will open. MATLAB has very good documentation, you will find the syntax, description and examples on how to use them.
Prashant Bhagat on 19 Jul 2022
Thanks dear.