Adding more than one greater/less than condition to an 'if statement'
10 views (last 30 days)
Show older comments
Simply trying to construct an if statement asking whether a longitude/latitude coordinate for an image is located within a rectangular section.
for i = 1:number_of_images
if (im_lat(i) > lat_D && im_lat(i) < lat_A) && (im_lon(i) > lon_A && im_lon(i) < lon_B)
...
end
end
Where A is the top left corner of the rectangle, B is the top right, and D is the bottom left. I'm curious if there is an easy way of revising this statement. There may also be functions that do this, such as inpolygon; however, I'm not sure how to integrate anything like this with my current format. I greatly appreciate any information you are able to provide.
0 Comments
Accepted Answer
dpb
on 7 Jun 2018
Edited: dpb
on 9 Jun 2018
inpolygon can do it but you've then got to return both output variables and do the set difference between in and on to get those inside as it returns inclusive of boundary and your test is written as exclusive. In addition, it's more verbose to define the vertices of the square than write the explicit test value.
I use a little helper function that is simply "syntactic sugar" to simplify such that lets you write the above as
if iswithinex(im_lat(i),lat_D,lat_A) & iswithinex(im_lon(i),lon_A,lon_B)
where iswithinex is
>> type iswithinex
function flg=iswithinex(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithinex(x,lo,hi)
% returns T for x between lo and hi values, exclusive
flg= (x>lo) & (x<hi);
>>
Originally I began with iswithin(x,lo,hi,flg) where the flag was for [in|ex]clusive but couldn't ever remember which way had defined it so I ended up with two specific versions. I suppose probably the way would have been to have made the flag input the string instead of a logical; in fact, I may try that variation and see how I like it... :)
6 Comments
dpb
on 12 Jun 2018
You're welcome...not following convention on zero for false will lead to much confusion for anyone else trying to follow your code and likely also eventually lead to logic errors as you'll continuously be fighting built in Matlab convention for every logical function there is--and there are a lot of 'em! :) Look at all the ISxxx() family for example.
More Answers (0)
See Also
Categories
Find more on Axis Labels 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!