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
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
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, :);
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!