Borrar filtros
Borrar filtros

how can i select specific rows and sum the values later

2 visualizaciones (últimos 30 días)
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 comentarios
John Chilleri
John Chilleri el 20 de Sept. de 2017
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).
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD el 20 de Sept. de 2017
thank you very much i will give it a try now

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 20 de Sept. de 2017
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 comentario
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD el 20 de Sept. de 2017
Mr Walter this is the second time you saved my life, you are like an Angel, thank you very much Sir

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by