simion1232006 / nctoolbox

Automatically exported from code.google.com/p/nctoolbox
0 stars 0 forks source link

Allowing -1 or NaN to mean "end" #38

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
When passing array indices, I don't know how to pass "end" as an argument.  Can 
we allow -1 or NaN to work the same as "end" in our indexing arguments?

url='http://geoport.whoi.edu/thredds/dodsC/coawst_2_2/fmrc/coawst_2_2_best.ncd';
nc=ncgeodataset(url);
nc{'s_rho'}(end)
ans =

   -0.0313

I would like 
nc{'s_rho'}(-1)
and 
nc{'s_rho'}(nan) 
to return the same result.

Original issue reported on code.google.com by rsignell on 1 Feb 2012 at 7:08

GoogleCodeExporter commented 8 years ago
Oops, I didn't mean "nan".  I meant "inf".   

So that would be "inf" or -1 would return the same as "end".

Original comment by rsignell on 1 Feb 2012 at 7:09

GoogleCodeExporter commented 8 years ago
I like the idea of negative indices counting back from the 'end'. Where -1 is 
the last element, -2 is the 2nd to last element, etc. Not sure how I feel about 
using 'Inf' or 'NaN' though. In my mind, passing in those values should cause 
an error.

Original comment by bschlin...@gmail.com on 1 Feb 2012 at 7:13

GoogleCodeExporter commented 8 years ago
Ill take a look at adding this. Probably the -1, etc. style since it would be 
familiar to python users anyway.

Original comment by crosb...@gmail.com on 1 Feb 2012 at 7:32

GoogleCodeExporter commented 8 years ago
I like Brian's idea of using -1, -2 to go backward from the end.  Let's go with 
that!

Original comment by rsignell on 1 Feb 2012 at 8:04

GoogleCodeExporter commented 8 years ago
I like Brian's idea of counting from the end with -1, -2.  Let's go with that!

Original comment by rsignell on 1 Feb 2012 at 8:05

GoogleCodeExporter commented 8 years ago
Apparently the indexing function we are using already accepted -1, but had a 
different functionality. I've modified it so that all of the below examples 
work:

>> g2 = v.grid_interop(1,-1); % last index
>> g2 = v.grid_interop(1,-5); % 4th from the end
>> g2 = v.grid_interop(1,-5:-1); % 4th through last from the end

Original comment by crosb...@gmail.com on 2 Feb 2012 at 3:50