Using a selection structure to extract points of intersection between two arrays
3 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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.
See Also
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!