Using a selection structure to extract points of intersection between two arrays

3 views (last 30 days)
Hi everyone,
I have two 360x340 arrays called lat and long. I want to find all of the intersecting points where lat is between 5 and 60, and where long is between 309 and 343. My plan is to create a 360x340 array and assign all of the values that meet that criteria a value of 1, otherwise assign all other lat/long combinations a value of zero.
I started writing this:
lat = netcdf.getVar(ncfile,5);
long = netcdf.getVar(ncfile,6);
Array = zeros(360,340);
if 5<=lat<=60 & 308<=long<=343
want: assign those data points a value of 1
else want: assign those data points a value of 0
end
Can anyone help me figure out how to assign the correct data points either a 1 or 0, depending on whether it meets my criteria? Also I'm not sure if my original if statement will work. Feel free to offer suggestions if you see any problems with that too. Thanks in advance.

Accepted Answer

dpb
dpb on 2 Mar 2014
ix=iswithin(lat,5,60) & iswithin(long,308,343);
done. :)
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
In general I'd probably code it by putting the ranges into variables rather than in the actual statement so that it's simpler to edit them for different choices.
  1 Comment
Shannon
Shannon on 2 Mar 2014
Yes, this works great, thank you! It outputs a 360x340 array of mostly zeros, and a few ones. Now I want to extract the temperature data from each point that is assigned a 1. The temperature variable is called tos.
SST = tos(at all points in ix that are = 1)
And then take the mean of those values.
Any ideas? Thanks again!

Sign in to comment.

More Answers (1)

dpb
dpb on 2 Mar 2014
Edited: dpb on 3 Mar 2014
SST=tos(ix);
Hint: Lookup "logical indexing" in the documentation
Tbar=mean(SST(:));

Categories

Find more on Data Types 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!