How to find the frequency of individual elements in a matrix?

6 views (last 30 days)
How do I find the frequencies of all the elements in the matrix attached and also draw a histogram knowing the frequencies of the elements?

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 4 Apr 2024
Edited: Dyuman Joshi on 4 Apr 2024
MATLAB has a in-built function for it - histogram, which does both the operations in single command -
s = load('micro2d (2).mat')
s = struct with fields:
Expression1: [150x150 double]
vec = s.Expression1(:).';
val = unique(vec);
h = histogram(vec, [val Inf])
h =
Histogram with properties: Data: [2180 3888 3888 3888 3888 3888 3888 4514 4718 4718 4718 4718 4718 4718 4718 4718 4718 4718 2133 2133 2133 802 3805 3805 3805 3805 3805 3805 ... ] (1x22500 double) Values: [76 18 25 114 36 62 50 76 114 150 52 66 76 34 55 35 33 57 53 101 60 38 42 32 74 31 13 50 78 42 2 105 39 56 109 3 10 63 56 127 68 113 57 45 23 ... ] (1x420 double) NumBins: 420 BinEdges: [20 24 45 50 84 102 105 114 129 133 135 136 145 157 178 193 194 201 210 212 233 270 280 286 335 356 370 371 382 396 407 410 412 413 422 437 ... ] (1x421 double) BinWidth: 'nonuniform' BinLimits: [20 Inf] Normalization: 'count' FaceColor: 'auto' EdgeColor: [0 0 0] Use GET to show all properties
%numbers
val
val = 1x420
20 24 45 50 84 102 105 114 129 133 135 136 145 157 178 193 194 201 210 212 233 270 280 286 335 356 370 371 382 396
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%corresponding frequencies
freq = h.Values
freq = 1x420
76 18 25 114 36 62 50 76 114 150 52 66 76 34 55 35 33 57 53 101 60 38 42 32 74 31 13 50 78 42
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!