Finding frequency of values in an array

Hi there
I have a 128x128x16 double array. Firstly, I would like to get the total number of values < zero in this array. Then, i would like to get the sum of the frequency of occurance of each value in this array. The array name is Phase.
thanks

 採用された回答

Star Strider
Star Strider 2019 年 7 月 23 日

2 投票

One approach:
negPhase = nnz(Phase < 0); % Number Of Values < 0
[uPhase,ia,ic] = unique(Phase);
tally = accumarray(ic, 1);
freqOccurrence = [uPhase, tally]; % Frequency Of Occurrenct Of Each Value
If you do not want the results sorted (since the unique function does this by default), use:
[uPhase,ia,ic] = unique(Phase, 'stable');
instead. The rest of the code is unchanged. (Note that this will conform to the MATLAB linear matrix indexing convention, so it will be a column vector, not a matrix.)

5 件のコメント

Jan
Jan 2019 年 7 月 23 日
I tried it with histcounts, but was frustrated about the change from histc, which still confuse me. n = histcounts(Phase, uPhase) replies numel(uPhase)-1 counts. This is documented, but not useful. I know, that accumarray does the trick, but I still hope, that I have overseen how histcounts can count the elements directly, like the name seems to imply.
Ahmad Alenezi
Ahmad Alenezi 2019 年 7 月 24 日
Thanks for your reply.
what does this psection mean:
[uPhase,ia,ic] = unique(Phase);
And, how can i sum all frequencies (only) as numbers ?
i used below script to sum the frequencies, but did not work and gives me (6831x2) double array:
sumfreq = sum(freqOccurrence, 2);
Star Strider
Star Strider 2019 年 7 月 24 日
This unique call:
[uPhase,ia,ic] = unique(Phase);
gets the unique values in ‘Phase’ and returns a sorted vector of them and index vector ‘ic’ for use with the accumarray call. It is necessary because of the way accumarray works, and returns its output.
Your assignment here:
sumfreq = sum(freqOccurrence, 2);
calculates the sum of the individual rows of ‘freqOccurrence’. If you want to sum the second column of ‘freqOccurrence’, do this:
sumfreq = sum(freqOccurrence(:, 2));
That should do what you want.
Leyla Elyasizad
Leyla Elyasizad 2023 年 7 月 26 日
Hey Guys
Thanks for your helpful comments but I tried both unique & tabulate for double values with 4 decimal places (0.0202, 0.0031,.....) and with both functions I get two 0.0031 with differenct occurence!
Do you have any idea why this happens?
Dyuman Joshi
Dyuman Joshi 2023 年 7 月 26 日
@Leyla Elyasizad, because both numbers do not appear to be the same. See below -
%Assign 0.3
y = 0.3
y = 0.3000
%Value st
sprintf('%0.42f', y)
ans = '0.299999999999999988897769753748434595763683'
When trying to compare floating point numbers, it is better to use a tolerance.
y = 0.12230455
y = 0.1223
sprintf('%0.42f', y)
ans = '0.122304549999999997988986422114976448938251'
%Set tolerance of 10^-6
tol = 1e-6;
%%Comparison
%Using equality
isequal(y,0.12330455)
ans = logical
0
%Using a tolerance
abs(y-0.12230455)<tol
ans = logical
1
With the same idea in mind, try uniquetol instead of unique

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

その他の回答 (1 件)

dipanka tanu
dipanka tanu 2020 年 5 月 1 日

0 投票

can you use tabulate operator of matlab ?

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by