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

Home » Public Forums » archive » transfer the specific data in array A to array B
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
transfer the specific data in array A to array B [message #88245] Wed, 02 April 2014 11:37 Go to next message
lczhang123 is currently offline  lczhang123
Messages: 5
Registered: March 2014
Junior Member
I got a question when I was trying to save the interested data from array A to a NEW array B.

j=0
FOR i=0,n-2 do begin

IF A[i+1]-A[i] gt 80 THEN BEGIN
B[j]=A[i]
j=j+1

ENDIF

ENDFOR

it seems theres something wrong with B[j]=A[i] should I define B as an array first? Or anyone has a better idea? I know it is a stupid question....sorry about that
Re: transfer the specific data in array A to array B [message #88246 is a reply to message #88245] Wed, 02 April 2014 11:46 Go to previous messageGo to next message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
On Wednesday, April 2, 2014 2:37:55 PM UTC-4, lczha...@gmail.com wrote:
> I got a question when I was trying to save the interested data from array A to a NEW array B.
>
>
>
> j=0
>
> FOR i=0,n-2 do begin
>
>
>
> IF A[i+1]-A[i] gt 80 THEN BEGIN
>
> B[j]=A[i]
>
> j=j+1
>
>
>
> ENDIF
>
>
>
> ENDFOR
>
>
>
> it seems theres something wrong with B[j]=A[i] should I define B as an array first? Or anyone has a better idea? I know it is a stupid question....sorry about that

This is the kind of situation where thinking in IDL vector notation is probably best. The IDL WHERE() function does all the work for you.

;; WHERE(expression) returns indices where expression is true
wh = where(A[1:*]-A[*] GT 80, ct)

;; Use WH as an index list for A[]
if ct GT 0 then B = A[wh]

Done!
Craig
Re: transfer the specific data in array A to array B [message #88247 is a reply to message #88245] Wed, 02 April 2014 11:51 Go to previous messageGo to next message
Andy Sayer is currently offline  Andy Sayer
Messages: 127
Registered: February 2009
Senior Member
Yes, you have to define the array B before you can attempt to fill its elements like that.

There are other ways you could code something like the above, although if you're a beginner then try first however makes sense to you. :) For example, this should work:

ndata=n_elements(a)

for_b=where(a[1:ndata-2]-a[0:ndata-1] gt 80,nvals)

if nvals gt 0 then b=a[for_b]

And in case you need it, nvals in the above is the same as the final value of j.

Hope this helps,

Andy

On Wednesday, April 2, 2014 2:37:55 PM UTC-4, lczha...@gmail.com wrote:
> I got a question when I was trying to save the interested data from array A to a NEW array B.
>
>
>
> j=0
>
> FOR i=0,n-2 do begin
>
>
>
> IF A[i+1]-A[i] gt 80 THEN BEGIN
>
> B[j]=A[i]
>
> j=j+1
>
>
>
> ENDIF
>
>
>
> ENDFOR
>
>
>
> it seems theres something wrong with B[j]=A[i] should I define B as an array first? Or anyone has a better idea? I know it is a stupid question....sorry about that
Re: transfer the specific data in array A to array B [message #88248 is a reply to message #88247] Wed, 02 April 2014 11:52 Go to previous messageGo to next message
Andy Sayer is currently offline  Andy Sayer
Messages: 127
Registered: February 2009
Senior Member
Ah, ninjad by Craig. :)

On Wednesday, April 2, 2014 2:51:00 PM UTC-4, AMS wrote:
> Yes, you have to define the array B before you can attempt to fill its elements like that.
>
>
>
> There are other ways you could code something like the above, although if you're a beginner then try first however makes sense to you. :) For example, this should work:
>
>
>
> ndata=n_elements(a)
>
>
>
> for_b=where(a[1:ndata-2]-a[0:ndata-1] gt 80,nvals)
>
>
>
> if nvals gt 0 then b=a[for_b]
>
>
>
> And in case you need it, nvals in the above is the same as the final value of j.
>
>
>
> Hope this helps,
>
>
>
> Andy
>
>
>
> On Wednesday, April 2, 2014 2:37:55 PM UTC-4, lczha...@gmail.com wrote:
>
>> I got a question when I was trying to save the interested data from array A to a NEW array B.
>
>>
>
>>
>
>>
>
>> j=0
>
>>
>
>> FOR i=0,n-2 do begin
>
>>
>
>>
>
>>
>
>> IF A[i+1]-A[i] gt 80 THEN BEGIN
>
>>
>
>> B[j]=A[i]
>
>>
>
>> j=j+1
>
>>
>
>>
>
>>
>
>> ENDIF
>
>>
>
>>
>
>>
>
>> ENDFOR
>
>>
>
>>
>
>>
>
>> it seems theres something wrong with B[j]=A[i] should I define B as an array first? Or anyone has a better idea? I know it is a stupid question....sorry about that
Re: transfer the specific data in array A to array B [message #88252 is a reply to message #88248] Wed, 02 April 2014 13:57 Go to previous message
Andy Sayer is currently offline  Andy Sayer
Messages: 127
Registered: February 2009
Senior Member
To follow up a message the original poster sent via email, the question was asked about how to define 'b' beforehand when you don't know how big it is. The short answer is you can't really. What you could do is define 'b' to be the same size as 'a', and then shrink it afterwards when you know how many good elements there are. For example, this:

b=a
b[*] = !values.f_nan

And then after transferring valid values from a to b:

is_good=where(finite(b),ngood)
if ngood ct 0 then b=b[is_good]

Andy

On Wednesday, April 2, 2014 2:52:07 PM UTC-4, AMS wrote:
> Ah, ninjad by Craig. :)
>
>
>
> On Wednesday, April 2, 2014 2:51:00 PM UTC-4, AMS wrote:
>
>> Yes, you have to define the array B before you can attempt to fill its elements like that.
>
>>
>
>>
>
>>
>
>> There are other ways you could code something like the above, although if you're a beginner then try first however makes sense to you. :) For example, this should work:
>
>>
>
>>
>
>>
>
>> ndata=n_elements(a)
>
>>
>
>>
>
>>
>
>> for_b=where(a[1:ndata-2]-a[0:ndata-1] gt 80,nvals)
>
>>
>
>>
>
>>
>
>> if nvals gt 0 then b=a[for_b]
>
>>
>
>>
>
>>
>
>> And in case you need it, nvals in the above is the same as the final value of j.
>
>>
>
>>
>
>>
>
>> Hope this helps,
>
>>
>
>>
>
>>
>
>> Andy
>
>>
>
>>
>
>>
>
>> On Wednesday, April 2, 2014 2:37:55 PM UTC-4, lczha...@gmail.com wrote:
>
>>
>
>>> I got a question when I was trying to save the interested data from array A to a NEW array B.
>
>>
>
>>>
>
>>
>
>>>
>
>>
>
>>>
>
>>
>
>>> j=0
>
>>
>
>>>
>
>>
>
>>> FOR i=0,n-2 do begin
>
>>
>
>>>
>
>>
>
>>>
>
>>
>
>>>
>
>>
>
>>> IF A[i+1]-A[i] gt 80 THEN BEGIN
>
>>
>
>>>
>
>>
>
>>> B[j]=A[i]
>
>>
>
>>>
>
>>
>
>>> j=j+1
>
>>
>
>>>
>
>>
>
>>>
>
>>
>
>>>
>
>>
>
>>> ENDIF
>
>>
>
>>>
>
>>
>
>>>
>
>>
>
>>>
>
>>
>
>>> ENDFOR
>
>>
>
>>>
>
>>
>
>>>
>
>>
>
>>>
>
>>
>
>>> it seems theres something wrong with B[j]=A[i] should I define B as an array first? Or anyone has a better idea? I know it is a stupid question....sorry about that
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Solving a non-linear equation
Next Topic: Anyone knows a better local extrema function?

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

Current Time: Wed Oct 08 13:49:45 PDT 2025

Total time taken to generate the page: 0.00557 seconds