find common elements of the same row
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Anuradha
il 21 Giu 2016
Risposto: Shameer Parmar
il 21 Giu 2016
hello, I need to find rows that have the same number in the same row eg (1 1).
- s1 0 0 0 1 0 0 1
- s2 1 0 0 0 0 1 0
How do I do that. I have an array of (40x1000) and I need to do that for every 2 columns and store the data in a separate file.
Thank you in advance
0 Commenti
Risposta accettata
Shameer Parmar
il 21 Giu 2016
You can do this..
let us consider, you have following input matrix..
A = [1 2 3 4 5 6 7 8 9; 1 2 3 4 3 2 2 5 7; 9 8 7 6 5 4 3 2 1; 1 2 3 4 5 6 7 8 9; 1 2 3 4 3 2 2 5 7]
A =
1 2 3 4 5 6 7 8 9
1 2 3 4 3 2 2 5 7
9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9
1 2 3 4 3 2 2 5 7
% as you want to check same numbers in rows, so you need to check it row wise as follows..
count = 1;
for i = 1:size(A,1)
if length(A(i,:)) ~= length(unique(A(i,:)))
rowIndex(count) = i;
count = count + 1 ;
end
end
Output will be the new array, which will give you the row numbers (indices), which is having the same numbers in it..
Output:
rowIndex =
2 5
0 Commenti
Più risposte (2)
Shameer Parmar
il 21 Giu 2016
This is another Answer from my side..
If you have array of 40x1000, and all row and column contains 0's and 1's..
and you want to check if any row contains more than one 1's irrespective of 0's...then you can do this..
count = 1;
for i = 1:size(A,1)
if sum(A(i,:)) >= 2
rowIndex(count) = i;
count = count + 1 ;
end
end
0 Commenti
Vedere anche
Categorie
Scopri di più su Operators and Elementary Operations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!