From: Priom Rahman on 29 Jul 2010 06:44 Hi guys, I have a matrix with about 50 yeats of data (approx 18000 rows) and 5 columns, The columns are organised as follows: year month day rain class of rain what i need to do is go through the rainfall at each date (+ - 7 days) and then compare the class of rain with the same date (+ - 7 days) for all the years and find the 6 closest values of rainfall. i.e if was looking at feb 7 1960 (and it was a class 2) i need to give my self a window of +- 7 days (i.e feb 8 - feb 22) for all the years 1960 - 2010 ---> find all the class 2 rains (from column 5) that fall in that window of (feb 8 - feb 22) and then find the 6 closest values (ie rain(a)15th feb 1960 (column 4) - class 2 rain in those 50 year windows) I need to know when these rainfall events happened- so not only do i need the numbers, i need the corresponding date they happened (say like the smallest differece was 40.0-39.8 = 0.2, and it happened on feb 20th 1975)... ect ect [i intend to apply bootstapping to these days later] and I need to go through all the days I was thinking of creating another martix which would store the 6 closest values and their position (or maybe addin to the tail of the current one) I'm a beginer in matlab!! So any help would be very greatly appreciated. Regards
From: Oleg Komarov on 29 Jul 2010 07:13 "Priom Rahman" <z3188254(a)unsw.edu.au> wrote in message <i2rm1n$oe6$1(a)fred.mathworks.com>... > Hi guys, > > I have a matrix with about 50 yeats of data (approx 18000 rows) and 5 columns, > The columns are organised as follows: > > year month day rain class of rain > > what i need to do is go through the rainfall at each date (+ - 7 days) and then compare the class of rain with the same date (+ - 7 days) for all the years and find the 6 closest values of rainfall. > > i.e if was looking at feb 7 1960 (and it was a class 2) i need to give my self a window of +- 7 days (i.e feb 8 - feb 22) for all the years 1960 - 2010 ---> find all the class 2 rains (from column 5) that fall in that window of (feb 8 - feb 22) and then find the 6 closest values (ie rain(a)15th feb 1960 (column 4) - class 2 rain in those 50 year windows) > > I need to know when these rainfall events happened- so not only do i need the numbers, i need the corresponding date they happened (say like the smallest differece was 40.0-39.8 = 0.2, and it happened on feb 20th 1975)... ect ect > > [i intend to apply bootstapping to these days later] > > and I need to go through all the days > > I was thinking of creating another martix which would store the 6 closest values and their position (or maybe addin to the tail of the current one) > > I'm a beginer in matlab!! So any help would be very greatly appreciated. > > Regards 1) Use ismember on the month 2) use ismember on the day 3) use logical & to combine the indexes you obtained from 1-2) and then use find on the combined index to retrieve the locations. 4) use the locations to get the rows from your database 5) fliupd(sortrows(...)) on 4 and get the first 6 rows Without an example of the input I can't go much in detail (don't post here the whole 18.000 rows!) Oleg
From: Priom Rahman on 29 Jul 2010 07:45 Hi Oleg, Thanks for replying so promptly, okay well here is a detailed example of what I'm trying to do: (in this example I will use +- 1 instead od 7) and I'll use 4 years assume my "current date" = 5th of January 1960 say this is a hypothetical portion of my matrix year, month, day, rain, class 1960, 1, 3, 40.5, 2 1960, 1, 4, 0, NaN 1960, 1, 5, 34, 4 1960, 1, 6, 36.5, 4 1960, 1, 7, 12, 1 (ect ect) 1961, 1, 3, 23, 1 1961, 1,4, 0, NaN 1961, 1, 5, 13, 2 1961, 1, 6, 17, 2 1961, 1, 7, 30, 4 (ect ect) 1962, 1, 3, 0, NaN 1962, 1,4, 0, NaN 1962, 1, 5, 0, NaN 1962, 1, 6, 0, NaN 1962, 1, 7, 2, 3 (ect ect) 1963, 1, 3, 18, 1 1963, 1,4, 23, 4 1963, 1, 5, 28, 4 1963, 1, 6, 35, 4 1963, 1, 7, 20, 2 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Since my date is 5th January 1960 -- (from above we see its a class 4) in 1960 in my window of +- 2, I only have other other class 4 rain (36.5) in 1961 in my window I only have 1 more class 4 (30) --> which happened on the 7/1/1961 in 1962 - i have no class 4 in my window and in 1963 - I have 3 class 4 rain (23, 28, 35) so in total all the class 4 i have --> (36.5, 30, 23, 28, 35) so if I were to find the closest 3 to january 5th 1960 (with a rain of 34) --> I would pick 36.5, 30, and 35. I need to know which dates these were also: so (6/1/1960), (7/1/1961) and (6/1/1963) so once I know that I need to go on and repeat it for the next day (6th of january 1960 ---> for a new window of +- 2 days and then do it for all the years - untill I look at all the dates (1800 times) I dont know if that helps, I'm stuck because I dont know if I should be creating another matrix to hold all this information (i.e the closest 3 values and their location - for each of the 1800 days).
From: Priom Rahman on 29 Jul 2010 08:00 ** just a correction to my prior post (in this example I will use +- 2 days as my window instead of 7)
From: Oleg Komarov on 29 Jul 2010 08:48
"Priom Rahman" <z3188254(a)unsw.edu.au> wrote in message <i2rqh7$dse$1(a)fred.mathworks.com>... > ** just a correction to my prior post > > (in this example I will use +- 2 days as my window instead of 7) % Your input In = [ 1960, 1, 3, 40.5, 2 1960, 1, 4, 0, NaN 1960, 1, 5, 34, 4 1960, 1, 6, 36.5, 4 1960, 1, 7, 12, 1 1961, 1, 3, 23, 1 1961, 1,4, 0, NaN 1961, 1, 5, 13, 2 1961, 1, 6, 17, 2 1961, 1, 7, 30, 4 1962, 1, 3, 0, NaN 1962, 1,4, 0, NaN 1962, 1, 5, 0, NaN 1962, 1, 6, 0, NaN 1962, 1, 7, 2, 3 1963, 1, 3, 18, 1 1963, 1,4, 23, 4 1963, 1, 5, 28, 4 1963, 1, 6, 35, 4 1963, 1, 7, 20, 2]; % Current date Cdate = datenum('5 jan 1960', 'dd mmm yyyy'); [Cyear Cmonth Cday] = datevec(Cdate); % Current class rain Cclrain = In(datenum(In(:,1:3)) == Cdate,5); % Tolerance in days tol = 2; % Find those rows of the same month, day+-tol and same class rain loc = find(In(:,2) == Cmonth & ismember(In(:,3),Cday-tol:Cday+tol) & In(:,5) == Cclrain); % Now i get this result [loc In(loc,:)] ans = |loc|year|month|day|rain|classrain| 3.00 1960.00 1.00 5.00 34.00 4.00 4.00 1960.00 1.00 6.00 36.50 4.00 10.00 1961.00 1.00 7.00 30.00 4.00 17.00 1963.00 1.00 4.00 23.00 4.00 18.00 1963.00 1.00 5.00 28.00 4.00 19.00 1963.00 1.00 6.00 35.00 4.00 What do you mean by closest? Oleg |