Weekend extraction in Matlab

对着背影说爱祢 提交于 2019-12-04 05:48:12

问题


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

so on

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

so on

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

回答1:


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.

Without further ado:

arrayAllDay = weekday(timeVectorDaily(:,1));
loc = arrayAllDay == 1 | arrayAllDay == 7;
weekendRows = timeVectorDaily(loc,:);

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.

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.



来源:https://stackoverflow.com/questions/24196869/weekend-extraction-in-matlab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!