I have a sequence of integers, say arr = [1,5,2,2,5,1]
.
I need a structure, say Counter
, that can tell me how many times the integer appears.
I have the following code but it won't work since isfield
cannot be used in this way.
for i = 1 : length(arr)
if ~isfield(Counter, i)
Counter{i} = 0;
end
Counter{i} = Counter{i} + 1
end
So is there any elegant way that can accumulate the number of appearance of an integer sequence?
The two core commands are
unique
andhistc
.