Finding the mean track of multiple years

2 views (last 30 days)
Ryne Noska
Ryne Noska on 11 Dec 2014
Answered: Guillaume on 11 Dec 2014
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 Comment
dpb
dpb on 11 Dec 2014
Edited: dpb on 11 Dec 2014
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.

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 11 Dec 2014
Creating a different variable for each year is not a good idea. You're better off storing each year in a cell array.
Anyway, to split your data per year, you could use the third return value of unique:
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)
You don't need to split the data per year to get the year average though, if you use accumarray:
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]

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!