現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Frequency distribution of monthly data
18 ビュー (過去 30 日間)
古いコメントを表示
I have a monthly timetable with some values and I need to calculate the frequency distribution in classes from 0 to 10 with step of 0.1. Right now using tabulate I receive these results.
frequency=(0:0.1:10);
tabulate(TT)
Value Count Percent
0 42832 95.95%
0.1 1549 3.47%
0.2 132 0.30%
0.3 48 0.11%
0.4 26 0.06%
0.5 16 0.04%
0.6 11 0.02%
0.7 2 0.00%
0.8 9 0.02%
0.9 4 0.01%
1 4 0.01%
1.1 3 0.01%
1.2 3 0.01%
1.3 1 0.00%
How can I fill the classes until 10 (even with zeros, in other months I expect to have higher values).
回答 (1 件)
Star Strider
2023 年 1 月 17 日
Define the edges as:
Ev = linspace(0, 10, 101)
If you want the frequency (rather than the default 'counts'), choose the 'probability' Normalization option.
.
8 件のコメント
Ancalagon8
2023 年 1 月 17 日
編集済み: Ancalagon8
2023 年 1 月 17 日
N = histcounts(TT.Var)
h=histogram(TT.Var,nbins)
h = findobj(gcf, 'Type', 'histogram');
values = h.Values; % Retrieve the Data of the histogram
Now I wonder how can i create a loop for the twelve months.
EDIT: uploaded TT.mat
Star Strider
2023 年 1 月 17 日
編集済み: Star Strider
2023 年 1 月 17 日
My pleasure!
I would just use something similar to the month loops that already exist. Those can either be one of the earlier ones, or one that reads the pages of the .xlsx file and computes the frequency histogram from those.
Also, there is no need to use the histogram function if you do not need the bar plot it creates. If you only want the counts or frequencies, use histcounts. Retain the edge output or edge vector calculation for reference.
If you provide the file you are currently using, or the month results in the .xlsx file, it would be relatively easy to code this. Since all the results will have the same number of rows (or columns), you could save the frequency results in a matrix rather than a cell array.
EDIT — (17 Jan 2023 at 16:36)
There is only one month, so that made robust coding to work with the entire year something of a challenge. This should work with the entire data set —
LD = load(websave('TT','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1266730/TT.mat'));
TT = LD.TT
TT = 44640×1 timetable
date_time Rain
__________________ ____
01-Jan-19 00-00-00 0
01-Jan-19 00-01-00 0
01-Jan-19 00-02-00 0
01-Jan-19 00-03-00 0
01-Jan-19 00-04-00 0
01-Jan-19 00-05-00 0
01-Jan-19 00-06-00 0
01-Jan-19 00-07-00 0
01-Jan-19 00-08-00 0
01-Jan-19 00-09-00 0
01-Jan-19 00-10-00 0
01-Jan-19 00-11-00 0
01-Jan-19 00-12-00 0
01-Jan-19 00-13-00 0
01-Jan-19 00-14-00 0
01-Jan-19 00-15-00 0
RainPerHour = retime(TT, 'hourly', 'sum')
RainPerHour = 744×1 timetable
date_time Rain
__________________ ____
01-Jan-19 00-00-00 0
01-Jan-19 01-00-00 0
01-Jan-19 02-00-00 0
01-Jan-19 03-00-00 0
01-Jan-19 04-00-00 0
01-Jan-19 05-00-00 0
01-Jan-19 06-00-00 0
01-Jan-19 07-00-00 0
01-Jan-19 08-00-00 0
01-Jan-19 09-00-00 0
01-Jan-19 10-00-00 0
01-Jan-19 11-00-00 0
01-Jan-19 12-00-00 0
01-Jan-19 13-00-00 0
01-Jan-19 14-00-00 0
01-Jan-19 15-00-00 0
for k = 1:12
MMidx = month(RainPerHour.date_time) == k;
RainPerHourMonth{k,:} = RainPerHour(MMidx,:);
end
for k = 1:12
TTTemp = RainPerHourMonth{k}; % Create Temporary 'timetable'
Hours = hour(TTTemp.date_time); % Create 'Hours' Variable
[y,m,d] = ymd(TTTemp.date_time); % Begin To Create 'Date' Variable
Date = datetime(y,m,d); % Finish Creating 'Date' Variable
TTTemp = addvars(TTTemp, Date, Hours,'Before','Rain'); % Add 'Hours' & 'Date' Variables
TTTemp.Properties.VariableNames(1:2) = {'Date','Hours'}; % Name 'Hours' & 'Date' Variables
TTTempT = timetable2table(TTTemp); % Convert To 'table'
RainPerHourMonthT{k,:} = unstack(TTTempT(:,2:end),'Rain','Hours', 'VariableNamingRule','preserve'); % Unstack & Write To Cell Array
end
RainPerHourMonthT % Display Results
RainPerHourMonthT = 12×1 cell array
{31×25 table}
{ 0×1 table}
{ 0×1 table}
{ 0×1 table}
{ 0×1 table}
{ 0×1 table}
{ 0×1 table}
{ 0×1 table}
{ 0×1 table}
{ 0×1 table}
{ 0×1 table}
{ 0×1 table}
RainPerHourMonthT{1}(1:5,:)
ans = 5×25 table
Date 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
___________ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ __ ___ ___ __ ___ ___ ___ ___ ___ ___ ___
01-Jan-2019 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
02-Jan-2019 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.2 0.1 0.1 0 0.3 0.3
03-Jan-2019 0.7 1.1 2.1 4.3 1.1 2.2 0.3 0.5 0.9 1.2 0.1 0.1 0 0 0 0 0 0 0.3 0 0 0 0 0
04-Jan-2019 0 0 0 0 0.2 0.1 0 0 0.1 3.2 0.1 0.5 0 0 0.3 0.5 0 1.6 0.9 0.3 0.5 0.2 0.1 0.1
05-Jan-2019 0.1 0.4 0 0.1 0.2 0.1 0 0 0 0 0 0 0.1 0 0 0 0 0 0 0 0 0 0 0
Edges = linspace(0, 10, 101);
Cntrs = Edges(1:end-1)+mean(diff(Edges));
for k = 1:numel(RainPerHourMonth)
[N,~,Bin] = histcounts(RainPerHourMonth{k}.Rain, Edges, 'Normalization','probability');
Nv{k,:} = N;
Binv{:,k} = Bin;
% MMM{k} = month(RainPerHourMonthT{k}.Date(1,:),'shortname');
end
Nv
Nv = 12×1 cell array
{[0.7527 0.0551 0.0228 0.0255 0.0108 0.0202 0.0054 0.0094 0.0027 0.0067 0.0067 0.0054 0.0027 0.0054 0 0.0040 0.0027 0.0027 0.0013 0.0027 0.0027 0.0054 0.0027 0.0027 0.0013 0.0067 0.0027 0.0013 0 0.0027 0 0.0040 0 0.0013 0 0.0013 0.0040 0 0 0 0 0.0013 0.0013 0.0013 0 0 0 0.0013 0 0 0.0013 0 0 0 0 0 0 0 0 0.0013 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0013 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0013 0]}
{[NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN]}
{[NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN]}
{[NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN]}
{[NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN]}
{[NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN]}
{[NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN]}
{[NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN]}
{[NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN]}
{[NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN]}
{[NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN]}
{[NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN]}
figure
tiledlayout(12,1, 'TileSpacing','none')
for k = 1:numel(RainPerHourMonth)
nexttile
bar(Cntrs, Nv{k})
grid
% ylabel(MMM{k})
ylim([0 1])
end
xlabel('Bin Centers')
Un-comment the commented lines to create month labels for the axes (that I hope will work with the complete data set).
.
Ancalagon8
2023 年 1 月 18 日
編集済み: Ancalagon8
2023 年 1 月 18 日
In part:
Edges = linspace(0, 10, 101);
Cntrs = Edges(1:end-1)+mean(diff(Edges));
for k = 1:numel(RainPerHourMonth)
[N,~,Bin] = histcounts(RainPerHourMonth{k}.Rain, Edges, 'Normalization','probability');
Nv{k,:} = N;
Binv{:,k} = Bin;
% MMM{k} = month(RainPerHourMonthT{k}.Date(1,:),'shortname');
end
Nv
I receive error Undefined function or variable 'RainPerHourMonth'.
Maybe because RainPerHourMonth is a 12X1 cell?
Also i think
RainPerHour = retime(TT, 'hourly', 'sum')
is not needed, maybe the initial TT values (per minute) should be kept and not get averaged per hour.
Star Strider
2023 年 1 月 18 日
I am guessing as to the variable names, and using the provided table (that has only one month of data).
I do not have the complete table you want to use, to use in my example code, and I am not certain that the other tables (previous tables that I do have access to) have the necessary information. They deal with ‘Temperature’ instead, and I do not know how they might otherwise differ from the rainfall tables.
If I have the correct data, I will do what I can to make my code work with it.
Ancalagon8
2023 年 1 月 18 日
You are right. I attach the initial yearly timetable (t).
Using the following code i splitted to 12 monthly timetables.
%Monthly timetables from t
for a = 1:12
MMidx = month(t.date_time) == a;
tMonth{a,:} = t(MMidx,:);
end
T = cell2table(tMonth);
r_January= T.tMonth{1,1};
r_February= T.tMonth{2,1};
r_March= T.tMonth{3,1};
r_April= T.tMonth{4,1};
r_May= T.tMonth{5,1};
r_June= T.tMonth{6,1};
r_July= T.tMonth{7,1};
r_August= T.tMonth{8,1};
r_September= T.tMonth{9,1};
r_October= T.tMonth{10,1};
r_November= T.tMonth{11,1};
r_December= T.tMonth{12,1};
Star Strider
2023 年 1 月 18 日
Having the entire timetable helps!
This is a somewhat streamlined version of my previous code —
LD = load(websave('t','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1267645/t.mat'));
t = LD.t
t = 513077×1 timetable
date_time Rain
__________________ ____
01-Jan-19 00-00-00 0
01-Jan-19 00-03-00 0
01-Jan-19 00-04-00 0
01-Jan-19 00-05-00 0
01-Jan-19 00-06-00 0
01-Jan-19 00-07-00 0
01-Jan-19 00-08-00 0
01-Jan-19 00-09-00 0
01-Jan-19 00-10-00 0
01-Jan-19 00-11-00 0
01-Jan-19 00-12-00 0
01-Jan-19 00-13-00 0
01-Jan-19 00-14-00 0
01-Jan-19 00-15-00 0
01-Jan-19 00-16-00 0
01-Jan-19 00-17-00 0
Rainmax = max(t.Rain)
Rainmax = 4.3000
Rainmin = min(t.Rain(t.Rain>0))
Rainmin = 0.1000
%Monthly timetables from t
for a = 1:12
MMidx = month(t.date_time) == a;
tMonth{a,:} = t(MMidx,:);
end
tMonth
tMonth = 12×1 cell array
{42923×1 timetable}
{40027×1 timetable}
{44449×1 timetable}
{43114×1 timetable}
{44542×1 timetable}
{41875×1 timetable}
{43988×1 timetable}
{44233×1 timetable}
{42688×1 timetable}
{44173×1 timetable}
{40673×1 timetable}
{40392×1 timetable}
% tMonth{1}
Edges = linspace(0, ceil(Rainmax), 101); % Change As Necessary To Produce The Desired REsults
Cntrs = Edges(1:end-1)+mean(diff(Edges));
for k = 1:numel(tMonth)
[N,~,Bin] = histcounts(tMonth{k}.Rain, Edges, 'Normalization','probability');
Nv{k,:} = N;
Binv{:,k} = Bin;
MMM{k} = month(tMonth{k}.date_time(1,:),'shortname');
end
Binmin = min(cellfun(@(x)min(x(x>0)),Nv))
Binmin = 2.2451e-05
Binmax = max(cellfun(@max,Nv))
Binmax = 1
figure
tiledlayout(6,2, 'TileSpacing','compact')
for k = 1:numel(tMonth)
nexttile
bar(Cntrs, Nv{k}, 'FaceColor','flat', 'EdgeColor','flat')
grid
ylabel(MMM{k})
ylim([1E-5 1]) % Optional, Change To Produce The Desired Result
set(gca,'YScale','log') % Optional (Shows Detail), Change To Produce The Desired Result
end
xlabel('Bin Centers')
Many of the bin counts are extremely small, and while they would be visible on a larger plot,.they are less visible on combined plots. After some experimentation, I decided on as the appropriate lower y-axis limit, based on the values of ‘Binmin’ and ‘Binmax’. (The month labeling works, too!)
.
Ancalagon8
2023 年 1 月 18 日
編集済み: Ancalagon8
2023 年 1 月 18 日
Thanks again! So the frequency distribution in classes from 0 to 10 with step of 0.1 for every month is stored in Nv? Because Nv is a 44640x1 cell, while I expected a 12x1 one.
Star Strider
2023 年 1 月 18 日
編集済み: Star Strider
2023 年 1 月 18 日
My pleasure!
Yes. The bins are an array that go from 0 rainfall to the maximum rainfall (‘ceil(Rainmax)’ so 5 here) in the entire timetable. The limits can be anything that works, so 10 is also an acceptable option for the upper limit of ‘Edges’. I chose the upper limit here to increase the resolution so the individual bars would be easier to see.
Also, the ‘Binv’ cell array stores the bin indices (in this instance, indices into the the dates and times for each month) that contributed to each bin. I don’t know if that’s important, however the histcounts function produces it, so I returned it and saved it.
EDIT — (18 Jan 2023 at 21:57)
When I re-ran my code just now and viewed it,, ‘Nv’ is a (12x1) cell array.
.
参考
カテゴリ
Help Center および File Exchange で Line Plots についてさらに検索
タグ
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)