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

조회 수: 2 (최근 30일)
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?

채택된 답변

Stephen23
Stephen23 2018년 7월 25일
편집: Stephen23 2018년 7월 25일
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
Guillaume
Guillaume 2018년 7월 25일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Guillaume
Guillaume 2018년 7월 25일
편집: Guillaume 2018년 7월 25일
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
Guillaume
Guillaume 2018년 7월 25일
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)

댓글을 달려면 로그인하십시오.


Udit Dhand
Udit Dhand 2018년 7월 25일
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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by