Finding frequency of values in an array
59 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
Star Strider
2019 年 7 月 23 日
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 件のコメント
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
2023 年 7 月 26 日
@Leyla Elyasizad, because both numbers do not appear to be the same. See below -
%Assign 0.3
y = 0.3
%Value st
sprintf('%0.42f', y)
When trying to compare floating point numbers, it is better to use a tolerance.
y = 0.12230455
sprintf('%0.42f', y)
%Set tolerance of 10^-6
tol = 1e-6;
%%Comparison
%Using equality
isequal(y,0.12330455)
%Using a tolerance
abs(y-0.12230455)<tol
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!