Instances of structure array with varying no. of elements [message #87364] |
Tue, 28 January 2014 07:47  |
midhunjoyp
Messages: 5 Registered: January 2014
|
Junior Member |
|
|
Hi all,
I have an array of structure arrays of the following simplified form :
results = REPLICATE({domain:REPLICATE({frame :fltarr(1), particles : fltarr(5,6000)}, 10)} , 500)
ie, 'domain' is initialized as a struct array with 10 elements(instances),
'results' is initialized as an array of 'domain' arrays with 500 instances
However during calculations, I would like to change the number of instances of 'domain' array in each instance of the 'results' array
eg: results[0] should contain only 5 instances of 'domain' array
results[1] should contain 7 instances of 'domain' array etc...
I tried doing the following for it :
results[0].domain = results[0].domain[0:4]
But, with:
print, n_elements(results[0].domain) : Returns value 10, which indicates that the size remains unchanged.
Is there any method for doing this operation ?
Best,
Midhun
|
|
|
Re: Instances of structure array with varying no. of elements [message #87366 is a reply to message #87364] |
Tue, 28 January 2014 08:10   |
Matthew Argall
Messages: 286 Registered: October 2011
|
Senior Member |
|
|
> I tried doing the following for it :
>
> results[0].domain = results[0].domain[0:4]
Structures tags have fixed sizes and types. Since results[0].domain is part of a structure and starts as a 10-element array, it will always be a 10.
The best way to do this is to put your "domain" structures into a list. Lists are dynamic and can change sizes/types. They have a weird way of indexing, though. The following example shows you how to put your structure into a list, then change element 0 of the list to have only 5 instances of "domain".
IDL> temp_results = REPLICATE({domain:REPLICATE({frame :fltarr(1), particles : fltarr(5,6000)}, 10)} , 500)
IDL> results = list(temporary(temp_results), /extract)
IDL> iKeep = [0,1,2,3,4]
IDL> results[0] = results[0,iKeep]
IDL> help, results[0]
<Expression> STRUCT = -> <Anonymous> Array[5]
This is a bit slow, so if you can put your domains directly into the list instead of extracting them, you will be better off.
|
|
|
|
Re: Instances of structure array with varying no. of elements [message #87370 is a reply to message #87364] |
Tue, 28 January 2014 08:53  |
midhunjoyp
Messages: 5 Registered: January 2014
|
Junior Member |
|
|
On Tuesday, January 28, 2014 10:47:40 AM UTC-5, midhu...@gmail.com wrote:
> Hi all,
>
>
>
> I have an array of structure arrays of the following simplified form :
>
>
>
> results = REPLICATE({domain:REPLICATE({frame :fltarr(1), particles : fltarr(5,6000)}, 10)} , 500)
>
>
>
> ie, 'domain' is initialized as a struct array with 10 elements(instances),
>
> 'results' is initialized as an array of 'domain' arrays with 500 instances
>
>
>
> However during calculations, I would like to change the number of instances of 'domain' array in each instance of the 'results' array
>
>
>
> eg: results[0] should contain only 5 instances of 'domain' array
>
> results[1] should contain 7 instances of 'domain' array etc...
>
>
>
> I tried doing the following for it :
>
> results[0].domain = results[0].domain[0:4]
>
>
>
> But, with:
>
> print, n_elements(results[0].domain) : Returns value 10, which indicates that the size remains unchanged.
>
>
>
> Is there any method for doing this operation ?
>
>
>
>
>
> Best,
>
> Midhun
Hi Matthew & David,
I had completely forgotten the concept of lists.
Thanks a lot for the help.
Best,
Midhun
|
|
|