Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

counting repetitions of values in a vector

574 views
Skip to first unread message

rupture

unread,
Feb 7, 2008, 10:31:43 AM2/7/08
to
Hi,

I need to write a function that takes a row vector as input, and counts the number of repetitions of each value in the vector, returning a structure with each value and the corresponding number of repetitions.

For example:

A = [1 1 1 2 2 2 2 3 3 3 3 3];
B = my_function(A)

val = [1 2 3]
rep = [3 4 5]

I am stuck on how to count the repetitions of each value. Advice please?

NOTE: This is for a piece of coursework I have to complete. Therefore, I do NOT want a code solution, I just want some advice as to how to tackle this problem.

Thanks in advance.

dpb

unread,
Feb 7, 2008, 10:43:18 AM2/7/08
to
rupture wrote:
> Hi,
>
> I need to write a function that takes a row vector as input, and
counts the number of repetitions of each value in the vector, returning
a structure with each value and the corresponding number of repetitions.
...

some hints...

lookfor hist
help unique

--

rupture

unread,
Feb 9, 2008, 12:29:11 PM2/9/08
to
Hi,

Thanks for the advice, though I didn't explain the problem very well the first time around!

The idea is to start with a row vector, such as:

X = [1 1 2 2 2 1 1 4 4 4 4]

And to produce an encoded version, represented as a structure such as:

Enc =

val: [1 2 1 4]
rep: [2 3 2 4]

..so that the original row vector could be reconstructed at a later date.


For the above vector, the 'unique' function will group both sets of 1s together so that using:

enc.val = unique(X);
enc.rep = histc(X,enc.val);

Will give me:

enc =

val: [1 2 4]
rep: [4 3 4]

Obviously, this is no good for reconstructing the original vector and I am stuck trying to think of a way around this as I am only just starting to learn Matlab.

Any other advice would be greatly appreciated.

Thanks

John D'Errico

unread,
Feb 9, 2008, 2:41:02 PM2/9/08
to
rupture <j24...@hotmail.com> wrote in message
<9964101.12025781822...@nitrogen.mathforum.org>...

There are several ways to do this.

If your vector is always integers, then accumarray
will work. If not, or you need a tolerance, then
my own consolidator will work. Find it on the
file exchange.

http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?
objectId=8354&objectType=file

You might also consider run length encoding,
look for it on the file exchange too.

http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?
objectId=6436&objectType=file

HTH,
john

Bruno Luong

unread,
Feb 9, 2008, 3:02:02 PM2/9/08
to
A=[4 1 1 1 4 4 4 2 1 4 2 4 2];
[val I J]=unique(A);
rep=diff(find(diff([-Inf sort(J) Inf])));
val
rep

rupture

unread,
Feb 9, 2008, 3:17:30 PM2/9/08
to
Hi,

as quoted from my previous reply:

Bruno Luong

unread,
Feb 9, 2008, 3:29:02 PM2/9/08
to
OK, is this what you want?


rep=diff(find(diff([-Inf A Inf])))
val=A(cumsum(rep))

Bruno

rupture

unread,
Feb 9, 2008, 3:48:49 PM2/9/08
to
Yes, thanks!

Only problem is, that I need to understand how it works and write it myself, otherwise it's plagiarism.

I understand how the val=A(cumsum(rep)) line works - it just indexes A at the last value in each repetition set, effectively forming a list of single values.

Can't quite figure out the rep=diff(find(diff([-Inf X Inf]))) line at the moment, but with a bit of thought I'm sure I can get my head around it. I'll post again if not.

Thanks again.

rupture

unread,
Feb 16, 2008, 1:19:41 PM2/16/08
to
OK. I get it now:

diff([-Inf A Inf]) finds out where the repetitions end, because elements where the next consecutive element is the same will cancel to 0.

find(diff([-Inf A Inf]) finds the indices of the nonzero elements, which are effectively the indices where the next new set of repetetive elements starts. The difference between these elements being a count of the number of elements in each set of repetition.

The A(cumsum(rep)) section just indexes the last element of each set of repetetive elements in A.

Cheers.

yousef.yo...@gmail.com

unread,
Apr 6, 2014, 12:14:21 PM4/6/14
to
HI,I have x=[2 4 4 1 1],I want if the number is not repeated like 2 to execute a condition.If repeated ,to execute another condition.

Nasser M. Abbasi

unread,
Apr 6, 2014, 12:40:44 PM4/6/14
to
On 4/6/2014 11:14 AM, yousef.yo...@gmail.com wrote:
> HI,I have x=[2 4 4 1 1],I want if the number is not
>repeated like 2 to execute a condition.If repeated ,to execute another condition.
>

if it is integer arrray, and not too big:

-----------------
x=[2 4 4 1 1];
I=histc(x,min(x):max(x));
if isempty(find(I==1))
'there are repeated elements'
else
'there is at least one non-repeated element'
end
---------------

yousefa...@gmail.com

unread,
Apr 6, 2014, 3:54:29 PM4/6/14
to

>
Thanks but I want the answer without using histc function

dpb

unread,
Apr 6, 2014, 4:37:00 PM4/6/14
to
On 4/6/2014 2:54 PM, yousefa...@gmail.com wrote:
>
>>
> Thanks but I want the answer without using histc function

Well, do it another way then... :)

--

Nasser M. Abbasi

unread,
Apr 6, 2014, 5:49:49 PM4/6/14
to
>> Thanks but I want the answer without using histc function

Ok, is using a loop accepted for the HW? May be you
can say what are the HW requirments so that one knows
how to answer it.

----------------------------------
x=[2 4 4 1 1];

iam_alone = false;

for i=1:length(x)
if not(ismember(x(i),[x(1:i-1) x(i+1:end)]))
iam_alone = true;
break;
end
end

if iam_alone
'no repeating found'
else
'repeating found'
end
----------------------




Chris

unread,
Apr 16, 2014, 7:54:10 PM4/16/14
to
This can be done elegantly using a bsxfun call:

val = unique(A);
rep = sum(bsxfun(@eq, val, A'), 1);



rupture <j24...@hotmail.com> wrote in message <21422604.1202398334...@nitrogen.mathforum.org>...
0 new messages