好吧...第一步是建立可以檢測在一個給定的日期落在本賽季的功能。幸運的是,我很早以前就已經開發出了能夠在南半球處理季節(這是逆轉的)。
該函數沒有執行任何完整性檢查,因爲我已經將它與已經過清理的數據集一起使用,但最終實現一些應該不難(除非您決定在使用它之前對數據集進行清理)。它以矢量化的方式工作,以最大化Matlab中計算的性能。
這裏是:
function season = GetSeason(date,southern_hemisphere)
if (nargin == 1)
southern_hemisphere = false;
end
[~,month,day] = datevec(date);
offset = month + (day/100);
winter = (offset < 3.21) | (offset >= 12.22);
spring = ~winter & (offset < 6.21);
summer = ~winter & ~spring & (offset < 9.23);
autumn = ~winter & ~spring & ~summer;
offset(spring) = 0;
offset(summer) = 1;
offset(autumn) = 2;
offset(winter) = 3;
if (southern_hemisphere)
offset = offset + 2;
end
season = mod(offset,4) + 1;
end
現在,第一步,你的腳本中,是從一個數據集文件中提取你的觀察。爲了爲您創建完整的演示,我創建了一個Excel
數據集。但你也可以使用一個CSV
數據集在用Matlab處理的代碼或其它格式的文件幾乎沒有變化:
% detect the dataset columns format
opts = detectImportOptions('data.xlsx');
% impose a specific format for the dataset columns
opts = setvartype(opts,{'datetime' 'double' 'double' 'double'});
% extract data in a table variable
data = readtable('data.xlsx',opts);
% sanitize the table variable removing the rows with missing or invalid values
data = rmmissing(data);
% sort the table variable rows by date (default first rows, default ascending)
data = sortrows(data);
第二次測試包括在獲得相應的季節爲觀察日期:
seasons = GetSeason(data.Date);
第三步,假設我們只對觀測的第一列稱爲Obs1
執行所有這一過程:
spring_1 = data.Obs1(seasons == 1);
summer_1 = data.Obs1(seasons == 2);
autumn_1 = data.Obs1(seasons == 3);
winter_1 = data.Obs1(seasons == 4);
第四步也是最後一步包括在一個圖表中繪製每個季節的一個箱形圖(必須將變量groups
作爲參數傳遞給boxplot
函數,以便知道後者需要繪製多少個箱子並使用哪些值):
groups = [
ones(size(spring_1));
2 * ones(size(summer_1));
3 * ones(size(autumn_1));
4 * ones(size(winter_1));
];
figure();
boxplot([spring_1; summer_1; autumn_1; winter_1],groups);
set(gca,'XTickLabel',{'Spring' 'Summer' 'Autumn' 'Winter'});
這裏是結果:
以飽滿的工作代碼更新所有意見
opts = detectImportOptions('data.xlsx');
opts = setvartype(opts,{'datetime' 'double' 'double' 'double'});
data = readtable('data.xlsx',opts);
data = rmmissing(data);
data = sortrows(data);
seasons = GetSeason(data.Date);
spring_1 = data.Obs1(seasons == 1);
summer_1 = data.Obs1(seasons == 2);
autumn_1 = data.Obs1(seasons == 3);
winter_1 = data.Obs1(seasons == 4);
spring_2 = data.Obs2(seasons == 1);
summer_2 = data.Obs2(seasons == 2);
autumn_2 = data.Obs2(seasons == 3);
winter_2 = data.Obs2(seasons == 4);
spring_3 = data.Obs3(seasons == 1);
summer_3 = data.Obs3(seasons == 2);
autumn_3 = data.Obs3(seasons == 3);
winter_3 = data.Obs3(seasons == 4);
plot_data = [
spring_1;
summer_1;
autumn_1;
winter_1;
spring_2;
summer_2;
autumn_2;
winter_2;
spring_3;
summer_3;
autumn_3;
winter_3
];
plot_groups = [
(1 * ones(size(spring_1))) (1 * ones(size(spring_1)));
(1 * ones(size(summer_1))) (2 * ones(size(summer_1)));
(1 * ones(size(autumn_1))) (3 * ones(size(autumn_1)));
(1 * ones(size(winter_1))) (4 * ones(size(winter_1)));
(2 * ones(size(spring_2))) (5 * ones(size(spring_2)));
(2 * ones(size(summer_2))) (6 * ones(size(summer_2)));
(2 * ones(size(autumn_2))) (7 * ones(size(autumn_2)));
(2 * ones(size(winter_2))) (8 * ones(size(winter_2)));
(3 * ones(size(spring_3))) (9 * ones(size(spring_3)));
(3 * ones(size(summer_3))) (10 * ones(size(summer_3)));
(3 * ones(size(autumn_3))) (11 * ones(size(autumn_3)));
(3 * ones(size(winter_3))) (12 * ones(size(winter_3)))
];
labels_obs = {'' '' '' '' '' '' '' '' '' '' '' ''};
labels_season = repmat({'Spring' 'Summer' 'Autumn' 'Winter'},1,3);
figure('Units','normalized','Position',[0.05 0.1 0.9 0.8]);
boxplot(plot_data,plot_groups, ...
'BoxStyle','outline', ...
'FactorGap',[5 1], ...
'Labels',{labels_obs; labels_season}, ...
'Notch','on');
colors = repmat('wcyg',1,3);
h = findobj(gca,'Tag','Box');
for i = 1:numel(h)
patch(get(h(i),'XData'),get(h(i),'YData'),colors(i),'FaceAlpha',0.5);
end
h = findall(allchild(findall(gca,'Type','hggroup')),'Type','text','String','');
positions = cell2mat(get(h,'pos'));
positions_new = num2cell([mean(reshape(positions(:,1),4,[]))' positions(1:4:end,2:end)],2);
set(h(1:4:end),{'Position'},positions_new,{'String'},{'Observations 3'; 'Observations 2'; 'Observations 1'})
h = findall(allchild(findall(gca,'Type','hggroup')),'Type','text','String','');
delete(h);
結果: