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

Home » Public Forums » archive » assign an array to an element in another array
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
assign an array to an element in another array [message #90644] Thu, 19 March 2015 16:37 Go to next message
lucesmm is currently offline  lucesmm
Messages: 26
Registered: October 2014
Junior Member
Hello
This is problem I have now,
my data now looks like this
10 94 30 63 0.248019426 240 75
91 29 24 74 0.177277796 418 98
129 27 25 86 0.959739977 268 92
140 17 26 85 0.812485863 457 84
152 71 20 55 0.384820239 369 83
176 68 27 104 0.295040055 211 57
196 89 21 79 0.841609784 278 89
292 100 28 87 0.547060688 309 96
420 58 24 99 0.21125357 311 77
459 17 24 84 0.687962261 229 65
478 79 23 73 0.811294052 219 96
498 65 27 79 0.869434322 447 59
573 65 23 89 0.009937773 209 67
585 54 21 95 0.074036581 283 103
627 76 30 104 0.444237881 275 77
636 32 24 93 0.588080971 232 74
646 18 20 74 0.206121808 278 104
759 23 20 102 0.799160185 276 77
763 16 20 81 0.427796559 226 93
783 59 30 101 0.416701726 255 79
801 66 20 86 0.60898004 346 93
832 84 29 58 0.160208725 219 55
900 8 23 67 0.872238518 411 92
949 91 20 91 0.393444055 490 66
997 15 27 82 0.058078121 399 60

I want to create 25 seprate vectors naming them with the forst element of each row... like this

vec10= [94 30 63 0.248019426 240 75]

vec91= [29 24 74 0.177277796 418 98]

vec129= [27 25 86 0.959739977 268 92]

vec140= [17 26 85 0.812485863 457 84]

vec152= [71 20 55 0.384820239 369 83]

vec176= [68 27 104 0.295040055 211 57]

vec196= [89 21 79 0.841609784 278 89]

vec292= [100 28 87 0.547060688 309 96]

vec420= [58 24 99 0.21125357 311 77]
...

how can I extract this values individually and then add the "VEC" portion to the name?

I have tried "foreach" , I have try to convert them to strings and add them together, and nothing yet.


Thank you

-LM*M
Re: assign an array to an element in another array [message #90645 is a reply to message #90644] Thu, 19 March 2015 18:56 Go to previous messageGo to next message
Dick Jackson is currently offline  Dick Jackson
Messages: 347
Registered: August 1998
Senior Member
On Thursday, 19 March 2015 16:37:29 UTC-7, luc...@gmail.com wrote:
> Hello
> This is problem I have now,
> my data now looks like this
> 10 94 30 63 0.248019426 240 75
> [...]
>
> I want to create 25 seprate vectors naming them with the forst element of each row... like this
>
> vec10= [94 30 63 0.248019426 240 75]
> ...
>
> how can I extract this values individually and then add the "VEC" portion to the name?
>
> I have tried "foreach" , I have try to convert them to strings and add them together, and nothing yet.
>
>
> Thank you
>
> -LM*M

There is almost always a better way to do what you want, rather than piecing together variable names. A hash table (or "hash") comes to mind.

myHash = Hash()

; then do this kind of thing in a loop:

myHash[10] = [94, 30, 63, 0.248019426, 240, 75]

The "key" (10) can be any scalar and "value" (the vector) can be anything at all. For more options beyond Hash, look to OrderedHash and Dictionary, which are subtly different.

Hope this helps!

Cheers,
-Dick

Dick Jackson Software Consulting Inc.
Victoria, BC, Canada --- http://www.d-jackson.com
Re: assign an array to an element in another array [message #90654 is a reply to message #90644] Fri, 20 March 2015 08:36 Go to previous messageGo to next message
wangchaoecnu is currently offline  wangchaoecnu
Messages: 1
Registered: March 2015
Junior Member
On Friday, March 20, 2015 at 12:37:29 AM UTC+1, luc...@gmail.com wrote:
> Hello
> This is problem I have now,
> my data now looks like this
> 10 94 30 63 0.248019426 240 75
> 91 29 24 74 0.177277796 418 98
> 129 27 25 86 0.959739977 268 92
> 140 17 26 85 0.812485863 457 84
> 152 71 20 55 0.384820239 369 83
> 176 68 27 104 0.295040055 211 57
> 196 89 21 79 0.841609784 278 89
> 292 100 28 87 0.547060688 309 96
> 420 58 24 99 0.21125357 311 77
> 459 17 24 84 0.687962261 229 65
> 478 79 23 73 0.811294052 219 96
> 498 65 27 79 0.869434322 447 59
> 573 65 23 89 0.009937773 209 67
> 585 54 21 95 0.074036581 283 103
> 627 76 30 104 0.444237881 275 77
> 636 32 24 93 0.588080971 232 74
> 646 18 20 74 0.206121808 278 104
> 759 23 20 102 0.799160185 276 77
> 763 16 20 81 0.427796559 226 93
> 783 59 30 101 0.416701726 255 79
> 801 66 20 86 0.60898004 346 93
> 832 84 29 58 0.160208725 219 55
> 900 8 23 67 0.872238518 411 92
> 949 91 20 91 0.393444055 490 66
> 997 15 27 82 0.058078121 399 60
>
> I want to create 25 seprate vectors naming them with the forst element of each row... like this
>
> vec10= [94 30 63 0.248019426 240 75]
>
> vec91= [29 24 74 0.177277796 418 98]
>
> vec129= [27 25 86 0.959739977 268 92]
>
> vec140= [17 26 85 0.812485863 457 84]
>
> vec152= [71 20 55 0.384820239 369 83]
>
> vec176= [68 27 104 0.295040055 211 57]
>
> vec196= [89 21 79 0.841609784 278 89]
>
> vec292= [100 28 87 0.547060688 309 96]
>
> vec420= [58 24 99 0.21125357 311 77]
> ...
>
> how can I extract this values individually and then add the "VEC" portion to the name?
>
> I have tried "foreach" , I have try to convert them to strings and add them together, and nothing yet.
>
>
> Thank you
>
> -LM*M

Are you trying to define variables dynamically?
see the information here. https://idldatapoint.wordpress.com/2012/10/11/dynamically-cr eating-variables/
Re: assign an array to an element in another array [message #90656 is a reply to message #90645] Fri, 20 March 2015 09:42 Go to previous messageGo to next message
Matthew Argall is currently offline  Matthew Argall
Messages: 286
Registered: October 2011
Senior Member
> There is almost always a better way to do what you want, rather than piecing together variable names.

Seconded. You can just create 6xN array, then access any row you want without all the trouble of naming them

;Create an array
array = randomu(seed, 6, 100)

;Pick a row
iRow = 4
theRow = array[*, iRow]


"iRow" is essentially the name of your row...
Re: assign an array to an element in another array [message #90657 is a reply to message #90644] Fri, 20 March 2015 11:01 Go to previous messageGo to next message
Russell[1] is currently offline  Russell[1]
Messages: 101
Registered: August 2011
Senior Member
I third the suggestions. There are very few instances where you *actually* want to create a variable dynamically, everything else is where you're doing it wrong. Dick's suggestion of a hash is good if you have the modern versions of IDL (>8) and Matt's is good if you're ok with all the data being the same type. But my suggestion is to make an array of structures.

(1) prototype your data with a structure. Just making it up, I'd say it's like this:

foo={FOO,name:0,x:0,y:0,z:0,val:0d,nx:0,ny:0}

(2) then make this an array:

data=replicate({foo},n)

where n is the number of rows in your data set

(3) fill your dataset:

openr,lun,'myfile',/get_lun
for i=0,n-1 do begin

readf,lun,ind,x,y,z,val,nx,ny,f=format

data(i)={foo,ind,x,y,z,val,nx,ny}
endfor
free_lun,pun


you'll need to set the variable "FORMAT" in the readf line to be what you need, but that should be pretty easy.

Now you can access the data like...


plot,data.x,data.y,ps=6

or some such stuff.

-Russell



On Thursday, March 19, 2015 at 7:37:29 PM UTC-4, luc...@gmail.com wrote:
> Hello
> This is problem I have now,
> my data now looks like this
> 10 94 30 63 0.248019426 240 75
> 91 29 24 74 0.177277796 418 98
> 129 27 25 86 0.959739977 268 92
> 140 17 26 85 0.812485863 457 84
> 152 71 20 55 0.384820239 369 83
> 176 68 27 104 0.295040055 211 57
> 196 89 21 79 0.841609784 278 89
> 292 100 28 87 0.547060688 309 96
> 420 58 24 99 0.21125357 311 77
> 459 17 24 84 0.687962261 229 65
> 478 79 23 73 0.811294052 219 96
> 498 65 27 79 0.869434322 447 59
> 573 65 23 89 0.009937773 209 67
> 585 54 21 95 0.074036581 283 103
> 627 76 30 104 0.444237881 275 77
> 636 32 24 93 0.588080971 232 74
> 646 18 20 74 0.206121808 278 104
> 759 23 20 102 0.799160185 276 77
> 763 16 20 81 0.427796559 226 93
> 783 59 30 101 0.416701726 255 79
> 801 66 20 86 0.60898004 346 93
> 832 84 29 58 0.160208725 219 55
> 900 8 23 67 0.872238518 411 92
> 949 91 20 91 0.393444055 490 66
> 997 15 27 82 0.058078121 399 60
>
> I want to create 25 seprate vectors naming them with the forst element of each row... like this
>
> vec10= [94 30 63 0.248019426 240 75]
>
> vec91= [29 24 74 0.177277796 418 98]
>
> vec129= [27 25 86 0.959739977 268 92]
>
> vec140= [17 26 85 0.812485863 457 84]
>
> vec152= [71 20 55 0.384820239 369 83]
>
> vec176= [68 27 104 0.295040055 211 57]
>
> vec196= [89 21 79 0.841609784 278 89]
>
> vec292= [100 28 87 0.547060688 309 96]
>
> vec420= [58 24 99 0.21125357 311 77]
> ...
>
> how can I extract this values individually and then add the "VEC" portion to the name?
>
> I have tried "foreach" , I have try to convert them to strings and add them together, and nothing yet.
>
>
> Thank you
>
> -LM*M
Re: assign an array to an element in another array [message #90658 is a reply to message #90654] Fri, 20 March 2015 11:20 Go to previous message
Fabzi is currently offline  Fabzi
Messages: 305
Registered: July 2010
Senior Member
On 20.03.2015 16:36, wangchaoecnu@gmail.com wrote:
> Are you trying to define variables dynamically?
> see the information here. (snip)

This post lacks this most important piece of information: it is barely
NEVER a good idea to define variables dynamically. I am still waiting
for someone showing a good example of this being a good idea. The post's
example (reading images) is still "bad practice" in my opinion...

Cheers,

Fabien
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: IDL Array Indices Command
Next Topic: IMSL won't load

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

Current Time: Wed Oct 08 13:33:00 PDT 2025

Total time taken to generate the page: 0.00659 seconds