how can I count the number of elements on a vector?
34 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Viridiana Torres
el 6 de Mayo de 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!!!
0 comentarios
Respuesta aceptada
CS Researcher
el 7 de Mayo de 2016
Editada: CS Researcher
el 7 de Mayo de 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 comentarios
Ahmet Cecen
el 7 de Mayo de 2016
Editada: Ahmet Cecen
el 7 de Mayo de 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
el 7 de Mayo de 2016
Editada: Stephen23
el 7 de Mayo de 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!
Más respuestas (1)
Ahmet Cecen
el 7 de Mayo de 2016
Editada: Ahmet Cecen
el 7 de Mayo de 2016
[C,ia,ic] = unique(T);
c = hist(ic,1:max(ic));
CountArray = [T(ia) c'];
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!