how can i select specific rows and sum the values later

2 次查看(过去 30 天)
Hi all; i have a problem to select every row. for eg: i have matrix 2600x25. one of the columns is the day of data, 1 to 31 representing each day in the month, and each number is repeated around 95 times, i want to select data for example for Monday from this month, how can matlab know that this number represents Monday of the month in 2007? and how can i choose the whole Mondays rows for example in this month, the expected number of rows will be 4*95 = around 400 rows representing that specific day .
thanks a lot in advance if somebody can help me.
regards
  2 个评论
John Chilleri
John Chilleri 2017-9-20
You might find the day function useful (can see documentation here). Otherwise, you can probably just identify the first Monday of 2007 and keep adding 7 using modulus for the months (brute force solution).

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2017-9-20
The following code assumes that you have a column that is just the day-of-month, with no column explicitly indicating month number or year number, and assumes that the way you know you have switched months is that the day number got smaller (e.g, 30 then 1)
%initialize
day_of_month_colnum = 7; %which column holds the day number?
startyear = 2007; %year being implicitly discussed
startmonth = 1; %which month number does the data start with?
%invent some data
test_day_of_month = reshape( [repelem(1:31,3),repelem(1:28,3),repelem(1:31,3)], [], 1);
YourData = rand(length(test_day_of_month), 25);
YourData(:,day_of_month_colnum) = test_day_of_month;
%start the work
DoMcol = YourData(:, day_of_month_colnum);
%calculate the relative month number for each entry
relmonth = cumsum([0;diff(DoMcol)<0]);
%build datevec information
entry_date = zeros(length(relmonth), 3);
entry_date(:,1) = startyear;
entry_date(:,2) = startmonth + relmonth;
entry_date(:,3) = DoMcol;
%turn datevec into weekday number
day_of_week = weekday( datenum(entry_date) );
%now select the Monday data out of the original data. Weeks start with Sunday
monday_data = YourData(day_of_week == 2, :);
  1 个评论
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD 2017-9-20
Mr Walter this is the second time you saved my life, you are like an Angel, thank you very much Sir

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by