How to find the length of intersection of rows of a matrix?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Voss
le 19 Juil 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 commentaires
Voss
le 20 Juil 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);
Plus de réponses (2)
Monica Roberts
le 19 Juil 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 commentaires
vishweshwar samba
le 19 Juil 2022
Modifié(e) : vishweshwar samba
le 19 Juil 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)
3 commentaires
vishweshwar samba
le 19 Juil 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.
Please follow these steps, if you still have difficulties please let me know.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!