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:
count how many times each set of number occurs

Subject: count how many times each set of number occurs

From: Jemma Geoghegan

Date: 18 Aug, 2010 01:26:05

Message: 1 of 13

Suppose I have an output which is a vector containing 4 numbers, which is set to loop 5 times eg.

Ans =
1 2 3 4
1 2 3 4
5 6 7 8
1 2 3 4
5 6 7 8

I need a function to count how many times each set of number occurs
eg.
1 2 3 4 occurs 3 times
5 6 7 8 occurs 2 times

Thanks!

Subject: count how many times each set of number occurs

From: Grzegorz

Date: 18 Aug, 2010 08:49:05

Message: 2 of 13

"Jemma Geoghegan" <jemma.geoghegan@gmail.com> wrote in message <i4fcrd$j7f$1@fred.mathworks.com>...
> Suppose I have an output which is a vector containing 4 numbers, which is set to loop 5 times eg.
>
> Ans =
> 1 2 3 4
> 1 2 3 4
> 5 6 7 8
> 1 2 3 4
> 5 6 7 8
>
> I need a function to count how many times each set of number occurs
> eg.
> 1 2 3 4 occurs 3 times
> 5 6 7 8 occurs 2 times
>
> Thanks!
Hi!
Try this code:

function [p counter] = findPattern(A)
[p n m] = unique(A,'rows');
counter = zeros(size(p,1),1);
for k=1:size(p,1)
counter(k) = sum(m==k);
disp([num2str(p(k,:)) ' occurs ' num2str(counter(k)) ' times'])
end

regards

Subject: count how many times each set of number occurs

From: J G

Date: 18 Aug, 2010 22:39:06

Message: 3 of 13

Thank you so much Grzegorz!

Subject: count how many times each set of number occurs

From: J G

Date: 18 Aug, 2010 23:47:19

Message: 4 of 13

Hi Grzegorz, I just have one quick question if you don't mind...

So your code...

[p n m] = unique(A,'rows');
counter = zeros(size(p,1),1);
for k=1:size(p,1)
counter(k) = sum(m==k);
disp([num2str(p(k,:)) ' occurs ' num2str(counter(k)) ' times'])
end

does exactly what I want, so now I would like to store the output in a table.
My code for this part looks like this...

f = figure('Position', [100 100 752 350]);
t = uitable('Parent', f, 'Position', [25 25 700 200]);
set(t, 'Data', complexData);

Where
complexData = the output from the display
but I'm not sure how to call this?

Thanks in advance if you can help!

Subject: count how many times each set of number occurs

From: Walter Roberson

Date: 19 Aug, 2010 02:55:22

Message: 5 of 13

J G wrote:
> Hi Grzegorz, I just have one quick question if you don't mind...
>
> So your code...
>
> [p n m] = unique(A,'rows');
> counter = zeros(size(p,1),1);
> for k=1:size(p,1)
> counter(k) = sum(m==k); disp([num2str(p(k,:)) ' occurs '
> num2str(counter(k)) ' times'])
> end
>
> does exactly what I want, so now I would like to store the output in a
> table. My code for this part looks like this...
>
> f = figure('Position', [100 100 752 350]);
> t = uitable('Parent', f, 'Position', [25 25 700 200]);
> set(t, 'Data', complexData);
>
> Where
> complexData = the output from the display
> but I'm not sure how to call this?

Before the loop:

complexData = cell(size(p,1),1);

In the loop:

complexData{k} = [num2str(p(k,:)) ' occurs ' num2str(counter(k))
' times'];

Subject: count how many times each set of number occurs

From: Walter Roberson

Date: 19 Aug, 2010 02:56:44

Message: 6 of 13

J G wrote:
> Hi Grzegorz, I just have one quick question if you don't mind...
>
> So your code...
>
> [p n m] = unique(A,'rows');
> counter = zeros(size(p,1),1);
> for k=1:size(p,1)
> counter(k) = sum(m==k); disp([num2str(p(k,:)) ' occurs '
> num2str(counter(k)) ' times'])
> end
>
> does exactly what I want, so now I would like to store the output in a
> table. My code for this part looks like this...
>
> f = figure('Position', [100 100 752 350]);
> t = uitable('Parent', f, 'Position', [25 25 700 200]);
> set(t, 'Data', complexData);
>
> Where
> complexData = the output from the display
> but I'm not sure how to call this?

Before the loop:

complexData = cell(size(p,1),1);

In the loop:

complexData{k} = [num2str(p(k,:)) ' occurs ' num2str(counter(k))
' times'];

Subject: count how many times each set of number occurs

From: Walter Roberson

Date: 19 Aug, 2010 03:00:18

Message: 7 of 13

J G wrote:
> Hi Grzegorz, I just have one quick question if you don't mind...
>
> So your code...
>
> [p n m] = unique(A,'rows');
> counter = zeros(size(p,1),1);
> for k=1:size(p,1)
> counter(k) = sum(m==k); disp([num2str(p(k,:)) ' occurs '
> num2str(counter(k)) ' times'])
> end
>
> does exactly what I want, so now I would like to store the output in a
> table. My code for this part looks like this...
>
> f = figure('Position', [100 100 752 350]);
> t = uitable('Parent', f, 'Position', [25 25 700 200]);
> set(t, 'Data', complexData);
>
> Where
> complexData = the output from the display
> but I'm not sure how to call this?

Before the loop:

complexData = cell(size(p,1),1);

In the loop:

complexData{k} = [num2str(p(k,:)) ' occurs ' num2str(counter(k))
' times'];

Subject: count how many times each set of number occurs

From: Walter Roberson

Date: 19 Aug, 2010 03:03:47

Message: 8 of 13

Walter Roberson wrote:
> J G wrote:
>> Hi Grzegorz, I just have one quick question if you don't mind...

Sorry, my posting software told me it didn't submit.

Subject: count how many times each set of number occurs

From: Walter Roberson

Date: 19 Aug, 2010 03:04:11

Message: 9 of 13

Walter Roberson wrote:
> J G wrote:
>> Hi Grzegorz, I just have one quick question if you don't mind...

Sorry, my posting software told me it didn't submit this one either.

Subject: count how many times each set of number occurs

From: Jos (10584)

Date: 19 Aug, 2010 11:14:05

Message: 10 of 13

"Grzegorz " <gknor@o2.pl> wrote in message <i4g6q1$74$1@fred.mathworks.com>...
> "Jemma Geoghegan" <jemma.geoghegan@gmail.com> wrote in message <i4fcrd$j7f$1@fred.mathworks.com>...
> > Suppose I have an output which is a vector containing 4 numbers, which is set to loop 5 times eg.
> >
> > Ans =
> > 1 2 3 4
> > 1 2 3 4
> > 5 6 7 8
> > 1 2 3 4
> > 5 6 7 8
> >
> > I need a function to count how many times each set of number occurs
> > eg.
> > 1 2 3 4 occurs 3 times
> > 5 6 7 8 occurs 2 times
> >
> > Thanks!
> Hi!
> Try this code:
>
> function [p counter] = findPattern(A)
> [p n m] = unique(A,'rows');
> counter = zeros(size(p,1),1);
> for k=1:size(p,1)
> counter(k) = sum(m==k);
> disp([num2str(p(k,:)) ' occurs ' num2str(counter(k)) ' times'])
> end
>
> regards

It is more efficient to use HISTC instead of a for-loop to count ...

A = [1 2 3 ; 1 2 4 ; 1 2 3 ; 1 2 5 ; 1 2 4 ]
[p n m] = unique(A,'rows');
counter = histc(m,1:numel(n))

Jos

Subject: count how many times each set of number occurs

From: Matt Fig

Date: 19 Aug, 2010 12:27:04

Message: 11 of 13

"Jos (10584) " <#10584@fileexchange.com> wrote in message
> It is more efficient to use HISTC instead of a for-loop to count ...
>
> A = [1 2 3 ; 1 2 4 ; 1 2 3 ; 1 2 5 ; 1 2 4 ]
> [p n m] = unique(A,'rows');
> counter = histc(m,1:numel(n))
>
> Jos

The OP has already been shown this (but didn't like it ;-)):

http://www.mathworks.com/matlabcentral/newsreader/view_thread/289563

Subject: count how many times each set of number occurs

From: Jos (10584)

Date: 19 Aug, 2010 13:13:07

Message: 12 of 13

"Matt Fig" <spamanon@yahoo.com> wrote in message <i4j7uo$rus$1@fred.mathworks.com>...
> "Jos (10584) " <#10584@fileexchange.com> wrote in message
> > It is more efficient to use HISTC instead of a for-loop to count ...
> >
> > A = [1 2 3 ; 1 2 4 ; 1 2 3 ; 1 2 5 ; 1 2 4 ]
> > [p n m] = unique(A,'rows');
> > counter = histc(m,1:numel(n))
> >
> > Jos
>
> The OP has already been shown this (but didn't like it ;-)):
>
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/289563

... double-posting ... sigh...

Subject: count how many times each set of number occurs

From: J G

Date: 19 Aug, 2010 23:05:09

Message: 13 of 13

Thank you for your help. How do I make it so each output is in a different column in the table?
Thanks again.

Tags for this Thread

No tags are associated with 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