How to find the length of intersection of rows of a matrix?
13 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
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)
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]);
2 Comments
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)
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);
More Answers (2)
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
0 Comments
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)
See Also
Categories
Find more on Transforms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!