Matlab中的周末提取 [英] Weekend extraction in Matlab
问题描述
我有一个大小为364 x 5的矩阵"timeVectorDaily".该矩阵具有十进制天形式的数据. 例如
i have a matrix "timeVectorDaily" that is 364 x 5 in size. This matrix has data in form of decimal days. Eg
734870 734870.2 734870.4 734870.6 734870.8
734871 734871.2 734871.4 734871.6 734871.8
依此类推
我只需要从"timeVectorDaily"中提取周末行.我编码以查找是否是周末的方式是使用工作日函数,该函数返回表示星期日至星期六的整数1到7.大小为364 x 1的数组"arrayAllDay"具有此信息. 例如
I need to extract only the weekend rows from "timeVectorDaily". The way i coded to find out if it was a weekend or not is by using the weekday function which returns integers 1 through 7 representing Sunday through Saturday. The array "arrayAllDay" that is 364 x 1 in size has this information. Eg
1
2
等等
我只进入了这个阶段.有人可以帮我从这里开始吗?我需要为每个周末提取1 X 5(工作日函数返回的1或7)
I have only gotten through to this stage.Can someone help me on how to proceed from here? I need to extract the 1 X 5 for every weekend (1 or 7 returned by the weekday function)
j = length(timeVectorDaily);
arrayAllDay = zeros(j,1);
counter = 0;
for m=1:j
[arrayAllDay(m)] = weekday(timeVectorDaily(m));
if arrayAllDay(m) == 1
counter = counter+1;
elseif arrayAllDay(m) == 7
counter = counter+1;
end
end
推荐答案
我们可以通过检查输入到此函数的矩阵的第一列是否输出一个weekday
函数中的哪个元素在周末内找到1
或7
.完成此操作后,这将为我们提供可以从timeVectorDaily
中选择的行.
We can find which elements in the weekday
function fall on a weekend by checking to see if the first column of your matrix when inputted into this function outputs a 1
or a 7
. Once we do this, this will give us which rows we can select out from timeVectorDaily
.
事不宜迟:
arrayAllDay = weekday(timeVectorDaily(:,1));
loc = arrayAllDay == 1 | arrayAllDay == 7;
weekendRows = timeVectorDaily(loc,:);
arrayAllDay
包含从1
到7
的数字,告诉您该数字在一周的哪一天. loc
是一个逻辑数组,其中1
表示一天是在周末,而0
则不是.最后,weekendRows
将包含与周末相对应的所有行.这样,这将生成一个M x 5
矩阵,其中M
是与矩阵第一列相对应的周末总数.
arrayAllDay
contains numbers from 1
to 7
that tell you what day of the week that number falls on. loc
is a logical array where 1
denotes that the day falls on a weekend and 0
where it doesn't. Finally, weekendRows
will contain all rows that correspond to weekends. As such, this will produce a M x 5
matrix, where M
are the total amount of weekends that correspond to the first column of your matrix.
顺便说一句,指的是@Shai,不建议将i
和j
用作迭代变量,因为这些变量用于表示复数.在此处查看此链接:在Matlab中使用i和j作为变量.这样做会掩盖这些复杂的变量,如果以后再进行任何复杂的分析,则可能会得到不可预测的结果.
BTW, referring to @Shai, it is inadvisable that you use i
and j
as iteration variables as these are used for representing complex numbers. See this link here: Using i and j as variables in Matlab . Doing so will shadow over these complex variables, and if you do any complex analysis later on, you may get unpredictable results.
这篇关于Matlab中的周末提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!