我是matlab的初学者。我正在做一个小项目,我面临有关过滤的问题 我在下面有一部分数据
'black' 11 '6/21/2013' <1x1 cell>
'blue' 11 '6/3/2013' <1x1 cell>
'yellow'12 '4/18/2015' <1x1 cell>
'white' 13 '11/11/2013'<1x1 cell>
'red' 14 '8/4/2014' <1x1 cell>
'blue' 15 '8/4/2014' <1x1 cell>
'yellow'16 '12/6/2014' <1x1 cell>
'red' 17 '10/4/2014' <1x1 cell>
'red' 18 '4/17/2015' <1x2 cell>
green' 19 '12/14/2014'<1x1 cell>
orange' 20 '3/18/2015' <1x1 cell>
数据的最后一列是一个单元格数组,其中包含一些值,如下所示:
<1x1 cell> 'b1'
<1x1 cell> 'c1'
<1x1 cell> 'b2'
<1x1 cell> 'c1'
<1x1 cell> 'b5'
<1x1 cell> 'd2'
<1x1 cell> 'f1'
<1x1 cell> 'f1'
<1x2 cell> 'b2' 'c1'
<1x1 cell> 'c1'
<1x1 cell> 'c1'
我想计算b1,c1,f1(整个数据集的值从a到z,a1到z1等等)的出现次数到用户选择的两个日期之间,输出为< / p>
from date:
to date:
count
f1
b1
c1
谢谢你的帮助。
答案 0 :(得分:0)
StartDate = input('What is the start date ','s'); % Prompt user for start date
EndDate = input('What is the end date ','s'); % Prompt user for end date
StartDate = datenum(StartDate); % Convert to datenum
EndDate = datenum(EndDate);
a=0;b=0;c=0; % Initalise counters
alphabet = {'a', 'b', 'c', ....} %EXPAND THIS TO Z
lettercount = zeros(1,numel(alphabet));
for ii = 1:size(YourCell,1)
celldat = datenum(YourCell{ii,3}); % Get date
if celldat > StartDate && celldat < EndDate % Check time boundaries
for jj = 1:size(YourCell{ii,4})
tmp = strcmp(YourCell{ii,4}{jj},alphabet);
lettercount = lettercount + tmp;
end
end
end
这里发生的是您首先提示用户输入您的时间间隔的开始和结束日期。然后,您将所有日期转换为datenum
以便于比较。初始化计数器,然后针对每个单元格根据提示日期检查日期,如果在该时间段内,则在lettercount
中收集字母,其中lettercount(1)=a
,lettercount(2)=b
,等