Find out the rows having same values in all columns of a matrix
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Hi,
I have a matrix with 4374 rows and 8 columns. I need to find the rows which have all the columns are of same values and it's repeated for 6 times.
As for example,
Let us consider a matrix, A= [ 0 0 0 0 1 0 0 ; 1 0 0 0 0 1 0; 1 0 0 0 0 1 0; 0 1 0 1 0 1 0; 1 0 0 0 0 1 0; 0 0 0 0 0 0 0; 1 0 0 0 0 1 0, 1 1 1 1 1 1 1; 1 0 0 0 0 1 0 , 0 0 1 1 0 1 0; 1 0 0 0 0 1 0; 0 0 0 0 1 0 0].
Here,0 0 0 0 1 0 0 is repeated twice and 1 0 0 0 0 1 0 is repeated for 6 times. Finally, I need to find and keep only those rows having repeated for 6 times (as like 1 0 0 0 0 1 0 in the given example) .
Can any please help me?
Accepted Answer
1 vote
Something like this should get you started:
>> A = [0,0,0,0,1,0,0;1,0,0,0,0,1,0;1,0,0,0,0,1,0;0,1,0,1,0,1,0;1,0,0,0,0,1,0;0,0,0,0,0,0,0;1,0,0,0,0,1,0;1,1,1,1,1,1,1;1,0,0,0,0,1,0;0,0,1,1,0,1,0;1,0,0,0,0,1,0;0,0,0,0,1,0,0]
A =
0 0 0 0 1 0 0
1 0 0 0 0 1 0
1 0 0 0 0 1 0
0 1 0 1 0 1 0
1 0 0 0 0 1 0
0 0 0 0 0 0 0
1 0 0 0 0 1 0
1 1 1 1 1 1 1
1 0 0 0 0 1 0
0 0 1 1 0 1 0
1 0 0 0 0 1 0
0 0 0 0 1 0 0
>> [U,~,idx] = unique(A,'rows');
>> cnt = histc(idx,unique(idx)) % count how many times rows occur.
cnt =
1
2
1
1
6
1
>> out = U(cnt==6,:) % pick which the unique rows which occur >= 6 times.
out =
1 0 0 0 0 1 0
If you want to get those multiple row instances, then try this:
>> idr = ismember(idx,find(cnt==6));
>> A(idr,:)
ans =
1 0 0 0 0 1 0
1 0 0 0 0 1 0
1 0 0 0 0 1 0
1 0 0 0 0 1 0
1 0 0 0 0 1 0
1 0 0 0 0 1 0
9 Comments
Sky Scrapper
on 26 Nov 2018
Edited: Sky Scrapper
on 26 Nov 2018
Yes, this is working. Thanks a lot!
Sky Scrapper
on 26 Nov 2018
But, I need to check if and only it's repeated for 6 times not more than 6 times. then it's showing nothing (void matrix)
Change the >= into == (as shown in my edited answer).
Sky Scrapper
on 26 Nov 2018
when i am running my original code, it's showing:
cnt= 54 in each rows having 81 total rows. Does it indicate that each row in my original matrix is repeated for 54 times?
"Does it indicate that each row in my original matrix is repeated for 54 times?"
The cnt values show how many times each unique row occurs in your matrix. For example, try doing this:
>> [cnt,U]
ans =
1 0 0 0 0 0 0 0
2 0 0 0 0 1 0 0
1 0 0 1 1 0 1 0
1 0 1 0 1 0 1 0
6 1 0 0 0 0 1 0
1 1 1 1 1 1 1 1
% cnt | unique rows
You can see for example, that row [0,0,0,0,1,0,0] occurs twice in A.
Sky Scrapper
on 26 Nov 2018
Edited: Sky Scrapper
on 26 Nov 2018
okay, got the answer. I have a matrix of 4374. after counting it's showing 81 rows and 54 times repeted. that means = (54*81)= 4374.
So, you are right. Thank you very much!
Sky Scrapper
on 24 Jan 2019
Edited: Sky Scrapper
on 24 Jan 2019
In my real matrix, i have,
A=[100 50 0 -100 0 0 0 0 1 0 0 80
100 70 0 0 0 1 0 0 0 0 1 88
100 -100 0 0 1 0 0 0 0 1 0 0
100 -100 100 0 0 1 0 1 0 1 0 95
100 100 0 -100 1 0 0 0 0 1 0 100
100 100 0 0 0 0 0 0 0 0 0 99
100 0 0 0 1 0 0 0 0 1 0 100
100 0 100 0 1 1 1 1 1 1 1 100
100 0 0 100 1 0 0 0 0 1 0 0
100 0 0 0 0 0 1 1 0 1 0 100
100 0 100 0 1 0 0 0 0 1 0 100
100 0 0 100 0 0 0 0 1 0 0 42]
I will have to delete rows having repeated values from column 5 to 11. For this, I am doing:
[U,~,idx] = unique(A(:,5:11),'rows');
Then it's showing only the valid rows havinng column 5 to 11 but I need the valid total rows.
Could you please help me?
Perhaps this is what you are looking for:
[~,idy,idx] = unique(A(:,5:11),'rows')
U = A(idy,:)
If not you will have to show what the expected output should be.
Sky Scrapper
on 24 Jan 2019
yes, that's I want. thank you so much!
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)