Use max() function in a CSV file for specific rows in a column

6 次查看(过去 30 天)
The structure of the file is:
Day JD Month State_id Year PRCP (in)
1 1 1 '352997' 1961 0
I'm looking for a way to get the maximum number in the PRCP column for each Month. It's 1961-2009 in the years column and 1-12 in the Month column. I had done the following:
i = untitled(1:95,1)
j = untitled(1:95,2)
while(i=1)
max(j)
end
My intention, to test out, was to see if the commands yielded the max in month 1 by looking through months 1-4. It kept giving me the maximum for the entire column, however, regardless of the month.

回答(1 个)

Walter Roberson
Walter Roberson 2012-11-13
moncol = 3; %according to the header
prcpcol = 6;
monnums = untitled(1:95, moncol);
prcps = untitled(1:95, prcpcol);
monmax = accumarray(monnums(:), prcps(:), [], @max, NaN);
This code assumes that when you say that you want the maximum for each month, that you are saying that (e.g.) March 1963 should be considered to be the same group of data as (say) March 2008 -- you did not say maximum for each month within each year. If there does not happen to be any data for a particular month (over all of the years) then NaN will appear.
If you want the maximum for each month within each year, then:
moncol = 3; %according to the header
yearcol = 5;
prcpcol = 6;
monnums = untitled(1:95, moncol);
yearnums = untitled(1:95, yearcol);
prcps = untitled(1:95, prcpcol);
firstyear = min(yearnums);
idx = [yearnums(:) - firstyear + 1, monnums(:)];
monmax = accumarray(idx, prcps(:), [], @max, NaN);
The result will be a 2D array, with (row number plus firstyear) being the year it relates to, and the column number being the month number. If there does not happen to be any data for a particular year+month combination, then NaN will appear.

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by