# how can I count the number of elements on a vector?

25 views (last 30 days)
Viridiana Torres on 6 May 2016
Edited: Stephen23 on 7 May 2016
Hello!!!!!
If I have a column lets imagine T=[105;105;105;106;106;106;107;107;107;107] how can I count the number of different elements in T? in this case 105 is 3 times, 106 appears 3 times and 107 is 4 times in T.
Thanks!!!

CS Researcher on 7 May 2016
Edited: CS Researcher on 7 May 2016
Try this:
a = unique(T);
b = arrayfun(@(x)(x-T),a,'UniformOutput',false);
c = cell2mat(cellfun(@(x)(numel(find(x==0))),b,'UniformOutput',false));
Hope this helps!
Stephen23 on 7 May 2016
Edited: Stephen23 on 7 May 2016
Ahmet Cecen is correct: this is a very bizarre, indirect, and totally inefficient solution. Ahmet's solution is much better, neater, and faster (for 1000 iterations):
Elapsed time is 1.607303 seconds. % this answer
Elapsed time is 0.489061 seconds. % Ahmet's answer
Lets take a look at this code in detail. The first line uses unique, which is a good start ( although it ignores the other much more useful outputs):
a = unique(T);
Then the weirdness starts:
b = arrayfun(@(x)(x-T),a,'UniformOutput',false);
giving a relatively large cell array of numeric matrices: this is a total waste of memory, as b is going to have an effective size of numel(T)*numel(a). Ouch, this could get very large! Use whos to check the size or variables in memory: for this answer:
>> whos
Name Size Bytes Class
T 10x1 80 double
a 3x1 24 double
b 3x1 420 cell <- ouch!
c 3x1 24 double
While it might not cause a problem for small input vectors, this will be a total waste of memory for larger inputs, as it will expand quite quickly.
At this point the numeric matrices have value zero replacing the values of interest.
This is then followed by a slow cellfun call on this large intermediate variable b:
c = cell2mat(cellfun(@(x)(numel(find(x==0))),b,'UniformOutput',false));
which checks where the zeros are in the numeric matrices inside b. Even here things are indirect:
numel(find(x==0))
should really be
nnz(x==0)
which would be much faster and simpler. But then, as Ahmet correctly pointed out, the concept is very indirect anyway: why not simply sum the indices in the first arrayfun anyway? Why bother converting the points of interest to zero, storing them in a huge matrix, and then checking counting those zeros by using find ?
Here is perhaps what the author really intended to write:
z = arrayfun(@(x)nnz(x==T),unique(T));
which takes half the time than the authors answer (although still not as fast as Ahmet's), and simply counts how many values of T match each unique value using one arrayfun call.

Ahmet Cecen on 7 May 2016
Edited: Ahmet Cecen on 7 May 2016
[C,ia,ic] = unique(T);
c = hist(ic,1:max(ic));
CountArray = [T(ia) c'];

### Categories

Find more on Spline Postprocessing in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by