[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: idl2matlab translate-o-matic

"J.D. Smith" <jdsmith@astro.cornell.edu> writes:

> Craig Markwardt wrote:
> >  * given two arrays, A and B: concatenate all but the last two
> >    elements of A, with B.  Don't try [A(0:n-3),B], or you will be in a
> >    world of hurt.
> > 
> I wouldn't say a *world* or hurt.  Maybe a minor planetesimal of hurt:
> C=n_elements(A)>2?[A[0:n_elements(A)-3],B]:B

Okay, my creative juices weren't flowing yet.  Consider that a warm-up

How about this one:

 * Given two 1-d arrays, A and B: insert B into any arbitrary position
   I in A.

I was hoping that it would be as easy as this, a = [a(0:i-1),b,a(i:*)], 
but then the special cases get start to be overwhelming (for example when i
equals 0 or n_elements(a)).  My point was that indexing with an empty
range should produce an empty list.

Instead you get this,

if i EQ 0 then begin
  a = [b, a]
endif else if i EQ n_elements(a)-1 then begin
  a = [a, b]
endif else begin
  a = [a(0:i-1),b,a(i:*)]

And if A or B are allowed to be empty or undefined at the start then
things get even *more* hurtful (perhaps even approaching a worldful).

These are all normal set-like things I'd like to do.  Thankfully there
are useful convenience routines like the Astronomy Library's
STORE_ARRAY, but somehow I think these issues could be better
addressed by making the language itself complete.


Craig B. Markwardt, Ph.D.         EMAIL:    craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response