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 many times repeat a number in an array??

Subject: how many times repeat a number in an array??

From: Maria

Date: 19 Nov, 2008 01:29:02

Message: 1 of 9

i have a function that gets from the user a random Array.by using the unique function i have a unique list with the arguments of the vector.with out using loop how do i find the times which a number is repeat in the array

Any help??

Subject: how many times repeat a number in an array??

From: Roger Stafford

Date: 19 Nov, 2008 01:51:02

Message: 2 of 9

"Maria " <mariaki16@hotmail.com> wrote in message <gfvq4u$goe$1@fred.mathworks.com>...
> i have a function that gets from the user a random Array.by using the unique function i have a unique list with the arguments of the vector.with out using loop how do i find the times which a number is repeat in the array
>
> Any help??

Check on the function 'histc'.

Roger Stafford

Subject: how many times repeat a number in an array??

From: Maria

Date: 19 Nov, 2008 02:19:02

Message: 3 of 9

can you explane me little the srtucture of histc becouse i don't understand it from the help ?

Subject: how many times repeat a number in an array??

From: Roger Stafford

Date: 19 Nov, 2008 02:48:01

Message: 4 of 9

"Maria " <mariaki16@hotmail.com> wrote in message <gfvt2m$hmp$1@fred.mathworks.com>...
> can you explane me little the srtucture of histc becouse i don't understand it from the help ?

  Hint: What interesting use can you make of the monotonically increasing output of 'unique'?

Roger Stafford

Subject: how many times repeat a number in an array??

From: Roger Stafford

Date: 19 Nov, 2008 04:00:04

Message: 5 of 9

"Maria " <mariaki16@hotmail.com> wrote in message <gfvq4u$goe$1@fred.mathworks.com>...
> i have a function that gets from the user a random Array.by using the unique function i have a unique list with the arguments of the vector.with out using loop how do i find the times which a number is repeat in the array
>
> Any help??

  An alternative and possbily superior hint is to use the third output argument of 'unique' in the 'accumarray' function rather than doing 'histc'. This has the advantage of requiring only one sort operation altogether.

Roger Stafford

Subject: how many times repeat a number in an array??

From: Maria

Date: 19 Nov, 2008 15:46:02

Message: 6 of 9

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gg0304$g8i$1@fred.mathworks.com>...
> "Maria " <mariaki16@hotmail.com> wrote in message <gfvq4u$goe$1@fred.mathworks.com>...
> > i have a function that gets from the user a random Array.by using the unique function i have a unique list with the arguments of the vector.with out using loop how do i find the times which a number is repeat in the array
> >
> > Any help??
>
> An alternative and possbily superior hint is to use the third output argument of 'unique' in the 'accumarray' function rather than doing 'histc'. This has the advantage of requiring only one sort operation altogether.
>
> Roger Stafford



what inputs doi have to put in accumarray and in histc in the histc help i found this:

N = HISTC(X,EDGES), for vector X, counts the number of values in X
    that fall between the elements in the EDGES vector (which must contain
    monotonically non-decreasing values). N is a LENGTH(EDGES) vector
    containing these counts.

what the eges should be???

Subject: how many times repeat a number in an array??

From: Roger Stafford

Date: 19 Nov, 2008 17:53:01

Message: 7 of 9

"Maria " <mariaki16@hotmail.com> wrote in message <gg1cbq$9gl$1@fred.mathworks.com>...
> what inputs doi have to put in accumarray and in histc in the histc help i found this:
>
> N = HISTC(X,EDGES), for vector X, counts the number of values in X
> that fall between the elements in the EDGES vector (which must contain
> monotonically non-decreasing values). N is a LENGTH(EDGES) vector
> containing these counts.
>
> what the eges should be???

  Maria, I will answer your questions this way. If you make a careful study of what the matlab functions 'histc' and/or 'accumarray' are intended to accomplish, then you will almost automatically realize what kind of inputs they require to solve your particular problem. This will probably require some experimentation on your part in the use of those functions in order to attain such an understanding.

  This is the way one best learns to use Matlab. Being given complete solutions without such an understanding is definitely NOT the right way since it leaves one unprepared for other problems.

Roger Stafford

Subject: how many times repeat a number in an array??

From: Hoi Wong

Date: 30 Mar, 2009 02:27:01

Message: 8 of 9

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gg1jpt$eja$1@fred.mathworks.com>...
> "Maria " <mariaki16@hotmail.com> wrote in message <gg1cbq$9gl$1@fred.mathworks.com>...
> > what inputs doi have to put in accumarray and in histc in the histc help i found this:
> >
> > N = HISTC(X,EDGES), for vector X, counts the number of values in X
> > that fall between the elements in the EDGES vector (which must contain
> > monotonically non-decreasing values). N is a LENGTH(EDGES) vector
> > containing these counts.
> >
> > what the eges should be???
>
> Maria, I will answer your questions this way. If you make a careful study of what the matlab functions 'histc' and/or 'accumarray' are intended to accomplish, then you will almost automatically realize what kind of inputs they require to solve your particular problem. This will probably require some experimentation on your part in the use of those functions in order to attain such an understanding.
>
> This is the way one best learns to use Matlab. Being given complete solutions without such an understanding is definitely NOT the right way since it leaves one unprepared for other problems.
>
> Roger Stafford

Thanks Roger! I've never given that much thought to the extended parameters for find(), sort() and a 5-argument use for sparse() though I've been using them for a while (sparse is quite new to me though. I'm developing a sparse_cell class/dataType and jengaMatrix is a piece of the puzzle)

My first algorithm was embarassing.......it's complicated, uses cellfun, sum, and runs 100000 times slower than any of yours, and it does not work for non-unique deletion list. Turns out that your 8-line solution is the fastest of all as I expected (because they are all built-in functions). Simple test is the matrix mentioned in my first post, and the second data set was captured from my project.

Here's the link to all of your algorithms and mine as well. jengaMatrixSpeedTest tests them all and generate the report below:

Does all algorithm give the same result for the simple test? true
---------------------------------------------------------------
Is the test matrices cardinal? true
Cardinality preserved? true
Is the deletion list unique? false
Roger: 0.00110589
Bruno1: 0.00870681
Bruno2: 0.02111930
Bruno4: 0.00462781
Bruno5: 0.00282878
Does all algorithm give the same result for the stress test (with non-unique deletion items)? true
Cardinality preserved? true
---------------------------------------------------------------
Hoi: 110.50091451
Roger: 0.00098781
Bruno1: 0.00648331
Bruno2: 0.00672234
Bruno4: 0.00160589
Bruno5: 0.00117171
Does all algorithm give the same result for the stress test (with unique deletion items)? true
Cardinality preserved? true

Subject: how many times repeat a number in an array??

From: Hoi Wong

Date: 30 Mar, 2009 02:32:01

Message: 9 of 9

"Hoi Wong" <wonghoi.ee@gmailNOSPAM.com> wrote in message <gqpall$2g8$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gg1jpt$eja$1@fred.mathworks.com>...
> > "Maria " <mariaki16@hotmail.com> wrote in message <gg1cbq$9gl$1@fred.mathworks.com>...
> > > what inputs doi have to put in accumarray and in histc in the histc help i found this:
> > >
> > > N = HISTC(X,EDGES), for vector X, counts the number of values in X
> > > that fall between the elements in the EDGES vector (which must contain
> > > monotonically non-decreasing values). N is a LENGTH(EDGES) vector
> > > containing these counts.
> > >
> > > what the eges should be???
> >
> > Maria, I will answer your questions this way. If you make a careful study of what the matlab functions 'histc' and/or 'accumarray' are intended to accomplish, then you will almost automatically realize what kind of inputs they require to solve your particular problem. This will probably require some experimentation on your part in the use of those functions in order to attain such an understanding.
> >
> > This is the way one best learns to use Matlab. Being given complete solutions without such an understanding is definitely NOT the right way since it leaves one unprepared for other problems.
> >
> > Roger Stafford
>
> Thanks Roger! I've never given that much thought to the extended parameters for find(), sort() and a 5-argument use for sparse() though I've been using them for a while (sparse is quite new to me though. I'm developing a sparse_cell class/dataType and jengaMatrix is a piece of the puzzle)
>
> My first algorithm was embarassing.......it's complicated, uses cellfun, sum, and runs 100000 times slower than any of yours, and it does not work for non-unique deletion list. Turns out that your 8-line solution is the fastest of all as I expected (because they are all built-in functions). Simple test is the matrix mentioned in my first post, and the second data set was captured from my project.
>
> Here's the link to all of your algorithms and mine as well. jengaMatrixSpeedTest tests them all and generate the report below:
>
> Does all algorithm give the same result for the simple test? true
> ---------------------------------------------------------------
> Is the test matrices cardinal? true
> Cardinality preserved? true
> Is the deletion list unique? false
> Roger: 0.00110589
> Bruno1: 0.00870681
> Bruno2: 0.02111930
> Bruno4: 0.00462781
> Bruno5: 0.00282878
> Does all algorithm give the same result for the stress test (with non-unique deletion items)? true
> Cardinality preserved? true
> ---------------------------------------------------------------
> Hoi: 110.50091451
> Roger: 0.00098781
> Bruno1: 0.00648331
> Bruno2: 0.00672234
> Bruno4: 0.00160589
> Bruno5: 0.00117171
> Does all algorithm give the same result for the stress test (with unique deletion items)? true
> Cardinality preserved? true

Oops....sorry. I replied to the wrong thread

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