Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to count how many times ech row has been repeated?

Subject: How to count how many times ech row has been repeated?

From: Jack

Date: 28 Jun, 2010 23:06:06

Message: 1 of 9

Hi,

I have a matrix consisting of a large number of rows. I need to know which rows has been repeated exactly the same more than once and how many times they have been repeated.
For Example if A= [ 1 2 3 ; 4 3 5; 1 2 3; 1 2 3; 4 3 5; 5 2 1; 3 2 1; 3 5 1]

then I should get something like the following:
1 2 3 : 3 times
4 3 5: 2 times

I wonder if there is an easy way to do it?
many thanks

Subject: How to count how many times ech row has been repeated?

From: Matt Fig

Date: 28 Jun, 2010 23:33:24

Message: 2 of 9

How about a hard way?

% Data
A= [ 1 2 3 ; 4 3 5; 1 2 3; 1 2 3; 4 3 5; 5 2 1; 3 2 1; 3 5 1]


% Engine
B = sortrows(A);
S = [1;any(diff(B),2)];
[L,S] = regexp(sprintf('%i',S'),'1(0)+','start','end');
repeated_rows = B(S,:) % Repeated Rows.
repeat_count = (S-L+1)' % How often each repeated row appears.

Subject: How to count how many times ech row has been repeated?

From: Jack

Date: 30 Jun, 2010 22:06:04

Message: 3 of 9

"Matt Fig" <spamanon@yahoo.com> wrote in message <i0bbg4$l1k$1@fred.mathworks.com>...
> How about a hard way?
>
> % Data
> A= [ 1 2 3 ; 4 3 5; 1 2 3; 1 2 3; 4 3 5; 5 2 1; 3 2 1; 3 5 1]
>
>
> % Engine
> B = sortrows(A);
> S = [1;any(diff(B),2)];
> [L,S] = regexp(sprintf('%i',S'),'1(0)+','start','end');
> repeated_rows = B(S,:) % Repeated Rows.
> repeat_count = (S-L+1)' % How often each repeated row appears.

My Dear;

Thanks alot for your response; I found it by coincedence. I did not receive an email "as usual" letting me know someone has responded... this is weired. I will go and check the code right now.

Many thanks

Subject: How to count how many times ech row has been repeated?

From: sscnekro

Date: 1 Jul, 2010 01:13:04

Message: 4 of 9

> Thanks alot for your response; I found it by coincedence. I did not receive an email "as usual" letting me know someone has responded... this is weired

Now I start to understand why some guys don't know about the help they really receive. They wait for e-mail instead of coming to the page. And it's also true that there were two or three cases when I e-mailed an alert on my nwsgr. reply (when the person was a first poster and seemed too desperate).

Subject: How to count how many times ech row has been repeated?

From: Jack

Date: 1 Jul, 2010 04:33:04

Message: 5 of 9

"Matt Fig" <spamanon@yahoo.com> wrote in message <i0bbg4$l1k$1@fred.mathworks.com>...
> How about a hard way?
>
> % Data
> A= [ 1 2 3 ; 4 3 5; 1 2 3; 1 2 3; 4 3 5; 5 2 1; 3 2 1; 3 5 1]
>
>
> % Engine
> B = sortrows(A);
> S = [1;any(diff(B),2)];
> [L,S] = regexp(sprintf('%i',S'),'1(0)+','start','end');
> repeated_rows = B(S,:) % Repeated Rows.
> repeat_count = (S-L+1)' % How often each repeated row appears.

Dear Matt;
Thanks a lot, this works well :) but it is really a hard way at least for me, I had to go to the help menu many times!! but how could I make change to the code if I want to list all the rows ( not only those repeated more than once)?
Best Regards

Subject: How to count how many times ech row has been repeated?

From: Matt Fig

Date: 1 Jul, 2010 04:47:04

Message: 6 of 9

What do you mean list all the rows? Can't the original matrix itself be seen as a list of all the rows? Do you mean list all rows without repeats listed? Look at the UNIQUE function, or if you want to combine this with the previous engine, put this in there:

B = sortrows(A);
S = [1;any(diff(B),2)];
Bu = B(logical(S),:); % List the unique elements of B
....

Subject: How to count how many times ech row has been repeated?

From: Oleg Komarov

Date: 1 Jul, 2010 09:21:05

Message: 7 of 9

Approach with unique:
[a,b,c] = unique(A,'rows');
% The last column lists the counts
[a accumarray(c,1)]

Oleg

Subject: How to count how many times ech row has been repeated?

From: Jack

Date: 1 Jul, 2010 18:15:11

Message: 8 of 9

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <i0hmm1$8hh$1@fred.mathworks.com>...
> Approach with unique:
> [a,b,c] = unique(A,'rows');
> % The last column lists the counts
> [a accumarray(c,1)]
>
> Oleg


Oleg; Thanks a lot; this was an easy code "to understand". The "unique" function is new for me.

Matt; thanks for intorducing me to the difficult ways of using matlab :) but seriously I learned a lot from your code.

Subject: How to count how many times ech row has been repeated?

From: Jack

Date: 3 Jul, 2010 12:26:05

Message: 9 of 9

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <i0hmm1$8hh$1@fred.mathworks.com>...
> Approach with unique:
> [a,b,c] = unique(A,'rows');
> % The last column lists the counts
> [a accumarray(c,1)]
>
> Oleg

One more question here;
how can I make the code counts rows [a,b,c] read the same in both directions as one single row (either one of the two). For example when after applying Oleg's code we get 3 4 5 6 which means 3 4 5 count is 6 and we get another row 5 4 3 10, then because 3 4 5 is read 5 4 3 in the opposite direction I want the code to give me 3 4 5 16 only ( or 5 4 3 16 only but noth both) where the 16 is the sum of two counts.

Thanks a lot

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us