How can I map to each vector entry a number telling its occurence so far?

1 view (last 30 days)
I have a vector
vec = [1,2,3,4,2,5,2,3];
and I want to have the output
[1,1,1,1,2,1,3,2]
so that each number tells me how often the value so far has occurred in the vector. Is there any possibility to do this without a for/while loop?

Accepted Answer

Stephen23
Stephen23 on 25 Jul 2018
Edited: Stephen23 on 25 Jul 2018
No third-party functions required, four lines of code:
>> vec = 5+[1,2,3,4,2,5,2,3];
>> [uni,~,idx] = unique(vec);
>> tmp = cell2mat(arrayfun(@(n)1:n,histc(vec,uni),'uni',0));
>> [~,idy] = sort(idx);
>> tmp(idy) % code output
ans =
1 1 1 1 2 1 3 2
>> [1,1,1,1,2,1,3,2] % requested output
ans =
1 1 1 1 2 1 3 2
  1 Comment
Guillaume
Guillaume on 25 Jul 2018
I would argue that arrayfun is a loop.
While it is indeed shorter, it is also about 4 times as slow on my machine. On the other hand both take less than half a second for a 1e6 element vector, so it doesn't really matter.

Sign in to comment.

More Answers (2)

Guillaume
Guillaume on 25 Jul 2018
Edited: Guillaume on 25 Jul 2018
It can be done without a loop, however for part of the code I recommend you use rcumsum from the FileExchange instead of what I've done here:
vec = [1,2,3,4,2,5,2,3];
[ordervec, indices ] = sort(vec); %group indentical values together and store their original position
issameasprevious = [0, ~diff(ordervec)];
%the following lines can be replaced by
%runlength = rcumsum(issameasprevious) + 1;
%otherwise:
zeroloc = ~issameasprevious;
runlength = cumsum(issameasprevious); %compute temporary cumsum with no reset
issameasprevious(zeroloc) = -diff([0 runlength(zeroloc)]); % how much to subtract at 0 values?
runlength = cumsum(issameasprevious) + 1; %recompute cumsum which no reset at 0
result = runlength(indices) %reorder according to original order
edit: typo in the code
  1 Comment
Guillaume
Guillaume on 25 Jul 2018
Note: my recommendation to use rcumsum apply if you're going to use the compiled mex version of it. It's optional and if you don't the code is exactly equivalent to what I've written (except it's going to be slower because of the input checks).
If you use the mex version of rcumsum then the above is going to be much faster than alternative solutions (and is also just 4 lines of code)

Sign in to comment.


Udit Dhand
Udit Dhand on 25 Jul 2018
I wasnt able to do it without using while loop
vec = [1,2,3,4,2,5,2,3];
j=length(vec);k=0;
while k~=j
o = find(vec==vec(j-k));
i=1:length(o);
out(o)=i;
k=k+1;
end

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!