이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Frequency distribution of monthly data
조회 수: 7 (최근 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!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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 (한국어)