Counting occurrences in order

조회 수: 2 (최근 30일)
Matt Fig
Matt Fig 2012년 9월 13일
I am wondering if anyone knows of a toolbox function that can perform this task quickly. I am not familiar with the Stats toolbox, but I have a feeling something could be there. Or maybe even the Image Processing Toolbox?
Here is the basic problem, and a naive solution. I want to count the occurrences of each unique item in a vector in the order in which they appear.
N = 5e5; % Typical _minimum_ size, max N = 5e7.
C = 50; % Best case shown, worst case: C = 1.5;
A = randi(floor(N/C),1,N); % Data looks like this.
% Now to produce our results. Need to find B.
Au = unique(A);
H = histc(A,Au);
B = zeros(size(A));
for ii = 1:length(Au)
B(A==Au(ii)) = 1:H(ii);
end
As a simple example for clarity, for N=10, C=3:
[A(:) B(:)]
ans =
3 1
2 1
3 2
3 3
3 4
2 2
3 5
3 6
3 7
1 1
I have written code that reduces the run-time by a large factor with the worst case C and minimum N, but it is a bit convoluted. After doing so I thought there might already be something like this out there, perhaps in a toolbox, but I couldn't find it. Perhaps there is a way to make further improvements for larger N.
Thanks!
EDIT I have not found any other solution to the this, so I may delete the question....
  댓글 수: 2
Sean de Wolski
Sean de Wolski 2012년 10월 4일
I would hazard a guess that removing unique() would be the biggest improvement. Though the one time cost might be worth it..
Ashish Uthama
Ashish Uthama 2012년 10월 5일
This is interesting. Thinking along the lines of using some kind of hash table. Please dont delete!

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

채택된 답변

Image Analyst
Image Analyst 2012년 10월 4일
I'm not sure why you want that but anyway it seems kind of esoteric/specialized. I can't think of anything in the Image Processing Toolbox that would get you the same thing as your code.
  댓글 수: 1
Matt Fig
Matt Fig 2012년 10월 4일
Thanks, IA. I thought there might be something either in the IPT or Stats. Perhaps not. Yes, it is very specialized ;-).

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

추가 답변 (1개)

Matt J
Matt J 2012년 10월 5일
편집: Matt J 2012년 10월 5일
  댓글 수: 1
Matt Fig
Matt Fig 2012년 10월 5일
Haha, Matt J! That is very similar. Perhaps my question is not as specialized as I thought. Thanks for the link, I don't know how I missed that question.

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

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by