comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » size(/dimen) that automatically fills in extra dimensions
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: size(/dimen) that automatically fills in extra dimensions [message #91242 is a reply to message #91241] Sun, 21 June 2015 00:42 Go to previous messageGo to previous message
Dick Jackson is currently offline  Dick Jackson
Messages: 347
Registered: August 1998
Senior Member
On Saturday, 20 June 2015 20:38:07 UTC-7, Jeremy Bailin wrote:
> Before I write a quick routine that does this, it seems like someone must have done this already:
>
> Does anyone have a drop-in replacement for SIZE(/DIMEN) that automatically fills in missing trailing dimensions with 1?
>
> I.e. I have an array A that is always 3xN, but N could be 1, 2, or 3. I want to find out N, but
>
> Size(A, /DIMEN)[1]
>
> fails if N eq 1 because IDL drops the final dimension.
>
> (even better: this would be a nice switch for the official SIZE function to have, if anyone is listening)
>
> -Jeremy.

Hi Jeremy,

I've been in your shoes...

In case this is helpful, there is a way to force the array to have a (3, N) shape, using Reform:

IDL> a=indgen(5,1)
IDL> help,a
A INT = Array[5] ; OK, the 1 has been dropped

IDL> a=reform(a,[5,1], /OVERWRITE)
IDL> help,a
A INT = Array[5, 1]

; Here's a handy routine when you want to ensure you have at least 'n' dimensions
;------
PRO EnsureNDims, x, nDims
IF Size(x, /N_Dimensions) GE nDims THEN RETURN
newDims = Replicate(1L, nDims)
newDims[0] = Size(x, /Dimensions) > 1 ; Will work even if x is scalar
x = Reform(x, newDims, /Overwrite)
END
;------

It can be interesting to see when this changes:

IDL> a=indgen([5,1])
IDL> help,a
A INT = Array[5]
IDL> ensurendims,a,2
IDL> help,a
A INT = Array[5, 1]
IDL> a=a
IDL> help,a
A INT = Array[5, 1]
; that was OK, didn't break it

IDL> b=a
IDL> help,b
B INT = Array[5]
; that broke it

IDL> a=b
IDL> help,a
A INT = Array[5]
; that broke 'a'

IDL> ensurendims,a,2
IDL> help,a
A INT = Array[5, 1]
IDL> a=a+1
IDL> help,a
A INT = Array[5]
; that broke it

IDL> a++
IDL> help,a
A INT = Array[5, 1]
; ... but that's OK!

Hope this helps!

Cheers,
-Dick

Dick Jackson Software Consulting Inc.
Victoria, BC, Canada --- http://www.d-jackson.com
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Previous Topic: formatting y-axis when plotting relative frequency using cgHistoplot
Next Topic: problem in reading unformatted data from a FORTRAN file

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 16:07:03 PDT 2025

Total time taken to generate the page: 0.00489 seconds