Finding the mean track of multiple years
2 次查看(过去 30 天)
显示 更早的评论
NA_tracks is a matrix of North Atlantic hurricane tracks with values of time (in years, repeating the same year through many consecutive cells before moving on chronologically), latitude, and longitude. I want to find the mean track of all the different years; that is, I would like to create individual variables for each year's track record and then average the variables resulting in a new mean track. Note: once locating a year's tracks by using the first column of NA_tracks, I want to delete the time column and only keep latitude and longitude for my separate year variables. Below is the script I have written that pertains to this question:
%%Combine separate variables into new array from 1985-2014
NA_tracks = zeros(13251,3);
NA_tracks(:,1) = time;
NA_tracks(:,2) = Latitude;
NA_tracks(:,3) = Longitude;
%%Extract SAWP years
% These years include:1985,1986,1989,1991,1992,1993,1994,1996,2002,2004
sawp_years = [1985,1986,1989,1991,1992,1993,1994,1996,2002,2004];
SAWP = NA_tracks(ismember(NA_tracks(:, 1), sawp_years), [2 3]);
This produces one matrix (SAWP) with tracks from all years included. I cannot take the mean of this code, because I will be left with one point (lat,lon) instead of the track (line of lats/lons) that I want. Lastly, all years have a different number of lats and lons.
Can anyone help me with this problem?
1 个评论
dpb
2014-12-11
编辑:dpb
2014-12-11
You've thrown away the pertinent information of which track is which by the global selection. What's the indicator in the original file that keeps track of each individual storm track sequence?
Use that and keep an indicator of the year and storm in some fashion or another...either as another running column variable or build a cell array that has a column for each track or somesuch.
Can't write exact code w/o knowing more detail of the original file.
ADDENDUM
I would tend to build a either a dataset or just use a plain 2D array but I'd fill the shorter tracks with NaN; then you can use nanMean and friends from the Statistics Toolbox or your own version thereof if don't have the toolbox.
回答(1 个)
Guillaume
2014-12-11
Creating a different variable for each year is not a good idea. You're better off storing each year in a cell array.
sawp_years = [1985,1986,1989,1991,1992,1993,1994,1996,2002,2004];
SAWP = [sawp_years(randi(numel(sawp_years), 100, 1))' (1:100)' (1:100)']; %random data for demo
[~, ~, yearbin] = unique(SAWP(:, 1)); %note that the first return argument is sawp_year.
yeartrack = arrayfun(@(bin) SAWP(yearbin == bin, [2 3]), 1:numel(sawp_years), 'UniformOutput', false)
sawp_years = [1985,1986,1989,1991,1992,1993,1994,1996,2002,2004];
SAWP = [sawp_years(randi(numel(sawp_years), 100, 1))' (1:100)' (1:100)']; %random data for demo
[~, ~, yearbin] = unique(SAWP(:, 1)); %note that the first return argument is sawp_year.
meanlat = accumarray(yearbin, SAWP(:, 2), [], @mean);
meanlong = accumarray(yearbin, SAWP(:, 3), [], @mean);
yearmean = [meanlat meanlong]
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!