Thread Subject: i need some help

Subject: i need some help

From: andreas

Date: 19 Nov, 2008 21:49:02

Message: 1 of 10

hello, is there anyway to find how many times an integer appears in a vector?

for example i have the vector [1 2 3 3 4 4 5 5]

and the answer should be [1 2 9 16 25]
when the number next to another is the same they multiply together

any hints?

Subject: i need some help

From: Matt Fig

Date: 19 Nov, 2008 22:40:17

Message: 2 of 10

It seems like you are asking two different questions. For the second one, you could do:


A = [1 2 3 3 4 4 5 5]; % Given vector
B = ~[1 diff(A)];
A(B) = A(B).^2;
A(circshift(B,[0 -1])) = []

Subject: i need some help

From: someone

Date: 19 Nov, 2008 22:48:02

Message: 3 of 10

"andreas " <thryloss2003@msn.com> wrote in message <gg21ke$3hi$1@fred.mathworks.com>...
> hello, is there anyway to find how many times an integer appears in a vector?
>
> for example i have the vector [1 2 3 3 4 4 5 5]
>
> and the answer should be [1 2 9 16 25]
> when the number next to another is the same they multiply together
>
> any hints?

% The question you asked is not the answer you are seeking.
% Do this integers always increase? (Is [ 1 2 3 3 2 1] allowed?)
% But, for hints try:

doc unique
doc diff
doc histc
doc sort

% That should get you started.

Subject: i need some help

From: andreas

Date: 19 Nov, 2008 23:07:02

Message: 4 of 10

"Matt Fig" <spamanon@yahoo.com> wrote in message <gg24kh$c5f$1@fred.mathworks.com>...
> It seems like you are asking two different questions. For the second one, you could do:
>
>
> A = [1 2 3 3 4 4 5 5]; % Given vector
> B = ~[1 diff(A)];
> A(B) = A(B).^2;
> A(circshift(B,[0 -1])) = []

yeah but the problem is that the program must do the multiplication also if three and more numbers are placed in the vector
for example [ 1 2 2 2 3 3 3 4 4 4]
should give [1 8 27 64]

Subject: i need some help

From: Matt Fig

Date: 19 Nov, 2008 23:27:01

Message: 5 of 10

> yeah but the problem is that the program must do the multiplication also if three and more numbers are placed in the vector
> for example [ 1 2 2 2 3 3 3 4 4 4]
> should give [1 8 27 64]

If your vectors are always sorted like this then you can use:

A = [ 1 2 2 2 3 3 3 4 4 4];
B = accumarray(A', A,[length(A),1],@prod,NaN);
B(isnan(B))=[]

Subject: i need some help

From: andreas

Date: 19 Nov, 2008 23:52:02

Message: 6 of 10

actually the question is this :

Write a Matlab function, named prodrep, that given a vector of numbers, computes the product of repeating
values in the vector. You should check the input is a vector and that it contain numbers. An example
result generated by your function might be:
>> vec = [1 2 2 2 3 3 2 2 2 2 1 1];
>> prodrep(vec)
ans =
1 8 9 16 1
For the example above, the first element is 1 (this value doesn’t repeat), the second element is given by
2  2  2, the third element by 3  3, the fourth by 2  2  2  2, and the last by 1  1. Similarly:
>> vec = [5 6 3 3 4 4 4 5 5 5 5 6 6 7];
>> prodrep(vec)
ans =
5 6 9 64 625 36 7

Subject: i need some help

From: MiCr0butty

Date: 20 Nov, 2008 00:36:02

Message: 7 of 10

just think about it!!!!=)

Subject: i need some help

From: Matt Fig

Date: 20 Nov, 2008 01:45:04

Message: 8 of 10

A = [ 6 6 1 2 2 2 5 5 5 1 4 4 4 4 4 2 2];
tmp = [1 diff(A)]~=0;
V = A(tmp).^diff(find([tmp 1]));

Subject: i need some help

From: andreas

Date: 20 Nov, 2008 01:51:02

Message: 9 of 10

"Matt Fig" <spamanon@yahoo.com> wrote in message <gg2ff0$9er$1@fred.mathworks.com>...
> A = [ 6 6 1 2 2 2 5 5 5 1 4 4 4 4 4 2 2];
> tmp = [1 diff(A)]~=0;
> V = A(tmp).^diff(find([tmp 1]));

thanx very much, but can you explain to me each one of the lines to understand what it's done by the program?;p

> tmp = [1 diff(A)]~=0;
> V = A(tmp).^diff(find([tmp 1]));

these two lines please...if you can

Subject: i need some help

From: Matt Fig

Date: 20 Nov, 2008 02:00:18

Message: 10 of 10

> thanx very much, but can you explain to me each one of the lines to understand what it's done by the program?;p
>
> > tmp = [1 diff(A)]~=0;
> > V = A(tmp).^diff(find([tmp 1]));

I can, but you have to learn something from this exercise! Just take apart what I wrote and look at each component, using the help for diff and find if necessary.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com