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

31 vues (au cours des 30 derniers jours)
Viridiana  Torres
Viridiana Torres le 6 Mai 2016
Modifié(e) : Stephen23 le 7 Mai 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!!!

Réponse acceptée

CS Researcher
CS Researcher le 7 Mai 2016
Modifié(e) : CS Researcher le 7 Mai 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!
  3 commentaires
Ahmet Cecen
Ahmet Cecen le 7 Mai 2016
Modifié(e) : Ahmet Cecen le 7 Mai 2016
This is reimplementing existing functionality in a roundabout way. Will be much slower (and require exponentially more memory) if you have on the order of millions or more numbers. If you just have 10s or 100s, then who cares, it still works.
Stephen23
Stephen23 le 7 Mai 2016
Modifié(e) : Stephen23 le 7 Mai 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.
Please read Ahmet's much better answer!

Connectez-vous pour commenter.

Plus de réponses (1)

Ahmet Cecen
Ahmet Cecen le 7 Mai 2016
Modifié(e) : Ahmet Cecen le 7 Mai 2016
[C,ia,ic] = unique(T);
c = hist(ic,1:max(ic));
CountArray = [T(ia) c'];

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by