How do I extract certain years from matrix?

1 view (last 30 days)
NA_tracks is a matrix with three columns: 1)year,2)longitude, and 3)latitude. I want only the longitude and latitude (columns 2 and 3) for specific years (1987,1998,1999,2001,2005,2006,2010,2012,2013,2014). Years are in chronological order, but there are many repeats of each. When I run the script below, I get a matrix LAWP of zeros. Does anyone have a suggestion on what I'm doing wrong and what I can do to get only the specified coordinate pairs?
%%Extract LAWP years
LAWP = zeros(length(NA_tracks),2); %Create empty matrix for LAWP year tracks
for i = 1:length(NA_tracks)
if NA_tracks(i,1) == 1987 || 1998 || 1999 || 2001 || 2005 || 2006 ...
|| 2010 || 2012 || 2013 || 2014;
LAWP(i,:) = NA_tracks(i,2:3);
elseif NA_tracks(i,1) == 1985 || 1986 || 1988 || 1989 || 1990 || 1991 ...
|| 1992 || 1993 || 1994 || 1995 || 1996 || 1997 || 2000 || 2002 ...
|| 2003 || 2004 || 2007 || 2008 || 2009 || 2011;
LAWP(i,:) = NaN;
end

Accepted Answer

Guillaume
Guillaume on 10 Dec 2014
Use ismember for this:
NA_tracks = [randi([1980 2015], 100, 1) (1:100)' (1:100)']; %random data for demo
years = [1987,1998,1999,2001,2005,2006,2010,2012,2013,2014];
LAWP = NA_tracks(ismember(NA_tracks(:, 1), years), [2 3]);
  1 Comment
Ryne Noska
Ryne Noska on 10 Dec 2014
Thank you for this incredibly simple-to-understand answer. It works perfectly.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!