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

48 ビュー (過去 30 日間)
Viridiana  Torres
Viridiana Torres 2016 年 5 月 6 日
編集済み: Stephen23 2016 年 5 月 7 日
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
CS Researcher 2016 年 5 月 7 日
編集済み: CS Researcher 2016 年 5 月 7 日
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 件のコメント
Ahmet Cecen
Ahmet Cecen 2016 年 5 月 7 日
編集済み: Ahmet Cecen 2016 年 5 月 7 日
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 2016 年 5 月 7 日
編集済み: Stephen23 2016 年 5 月 7 日
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!

サインインしてコメントする。

その他の回答 (1 件)

Ahmet Cecen
Ahmet Cecen 2016 年 5 月 7 日
編集済み: Ahmet Cecen 2016 年 5 月 7 日
[C,ia,ic] = unique(T);
c = hist(ic,1:max(ic));
CountArray = [T(ia) c'];

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by