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

Home » Public Forums » archive » Adding elements in an 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
Adding elements in an Array [message #85967] Sun, 22 September 2013 11:31 Go to next message
bhattacharjee12 is currently offline  bhattacharjee12
Messages: 3
Registered: September 2013
Junior Member
Hi,
I am new to IDl. I was hoping if you guys can help with my problem.
I will explain my problem with an example.
Lets say I have an array
x=[1,2,3,4,5,6,1,2,3]
& I want to average lets say every 3 elements and put the results in a new array say y
Where y would look like
y=[2,5,2]
Is there a nifty IDL function/easy way which would do that?
Thanks for the help in advance.
Ani
Re: Adding elements in an Array [message #85968 is a reply to message #85967] Sun, 22 September 2013 11:46 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
bhattacharjee12@gmail.com writes:

>
> Hi,
> I am new to IDl. I was hoping if you guys can help with my problem.
> I will explain my problem with an example.
> Lets say I have an array
> x=[1,2,3,4,5,6,1,2,3]
> & I want to average lets say every 3 elements and put the results in a new array say y
> Where y would look like
> y=[2,5,2]
> Is there a nifty IDL function/easy way which would do that?

IDL> x=[1,2,3,4,5,6,1,2,3]
IDL> x = reform(x, 3, 3)
IDL> b = Mean(x, dimension=1)
IDL> print, b
2.00000 5.00000 2.00000

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: Adding elements in an Array [message #85971 is a reply to message #85968] Sun, 22 September 2013 12:13 Go to previous messageGo to next message
bhattacharjee12 is currently offline  bhattacharjee12
Messages: 3
Registered: September 2013
Junior Member
Thanks a lot, David. Reform will be a great help.
Re: Adding elements in an Array [message #85983 is a reply to message #85967] Mon, 23 September 2013 10:46 Go to previous messageGo to next message
natha is currently offline  natha
Messages: 482
Registered: October 2007
Senior Member
y = x[1:*:3]

cheers,

nata
Re: Adding elements in an Array [message #85984 is a reply to message #85983] Mon, 23 September 2013 11:30 Go to previous messageGo to next message
wlandsman is currently offline  wlandsman
Messages: 743
Registered: June 2000
Senior Member
On Monday, September 23, 2013 1:46:09 PM UTC-4, nata wrote:
> y = x[1:*:3]

That takes every 3rd element starting with element 1. It gives the right answer in this case but just by coincidence, e.g. element 1 just happens to be the mean of the first 3 values, and element 4 happens to be the average of the second 3 values.

My solution would be

IDL> y = rebin(x,3)

--Wayne
Re: Adding elements in an Array [message #85985 is a reply to message #85984] Mon, 23 September 2013 12:37 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
wlandsman writes:

> That takes every 3rd element starting with element 1. It gives the right answer in this case but just by coincidence, e.g. element 1 just happens to be the mean of the first 3 values, and element 4 happens to be the average of the second 3 values.
>
> My solution would be
>
> IDL> y = rebin(x,3)

It's not clear to me that nearest neighbor sampling is the same as the
"mean" value in general, although it works in this specific case.

Cheers,

David



--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: Adding elements in an Array [message #85986 is a reply to message #85985] Mon, 23 September 2013 13:14 Go to previous messageGo to next message
wlandsman is currently offline  wlandsman
Messages: 743
Registered: June 2000
Senior Member
On Monday, September 23, 2013 3:37:57 PM UTC-4, David Fanning wrote:

>
> It's not clear to me that nearest neighbor sampling is the same as the
>
> "mean" value in general, although it works in this specific case.
>

I agree. That is why one shouldn't use the /SAMPLE keyword to REBIN() ;-) But as the documentation for REBIN says

"Normally, REBIN uses bilinear interpolation when magnifying and neighborhood averaging when minifying." REBIN only uses nearest neighbor sampling if the /SAMPLE keyword is set.

--Wayne


>
> Cheers,
>
>
>
> David
>
>
>
>
>
>
>
> --
>
> David Fanning, Ph.D.
>
> Fanning Software Consulting, Inc.
>
> Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
>
> Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: Adding elements in an Array [message #85987 is a reply to message #85986] Mon, 23 September 2013 13:31 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
wlandsman writes:

> "Normally, REBIN uses bilinear interpolation when magnifying and neighborhood averaging when minifying."

I suppose it would take me a couple of days scratching my head to figure
this out!

Do you know this to be true, or are you just taking their word for it?
I'm not sure "neighborhood averaging" means what you appear to think it
means. If you are right, it certainly doesn't mean what I think it
means! And, in any case, I wouldn't bet the satellite on it until I ran
a couple of tests. :-)

Cheers,

David



--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: Adding elements in an Array [message #85988 is a reply to message #85987] Mon, 23 September 2013 13:36 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
David Fanning writes:

>
> wlandsman writes:
>
>> "Normally, REBIN uses bilinear interpolation when magnifying and neighborhood averaging when minifying."
>
> I suppose it would take me a couple of days scratching my head to figure
> this out!
>
> Do you know this to be true, or are you just taking their word for it?
> I'm not sure "neighborhood averaging" means what you appear to think it
> means. If you are right, it certainly doesn't mean what I think it
> means! And, in any case, I wouldn't bet the satellite on it until I ran
> a couple of tests. :-)

I guess you are right. Wow! I learn something every day about IDL!

IDL> a = [10, 20, 5, 6, 18, 17, 1, 30, 2]
IDL> print, rebin(a, 3)
11 13 11
IDL> print, rebin(a, 3, /sample)
10 6 1


Cheers,

David

--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: Adding elements in an Array [message #85989 is a reply to message #85987] Mon, 23 September 2013 13:40 Go to previous messageGo to next message
wlandsman is currently offline  wlandsman
Messages: 743
Registered: June 2000
Senior Member
On Monday, September 23, 2013 4:31:00 PM UTC-4, David Fanning wrote:

>
> Do you know this to be true, or are you just taking their word for it?

I use this property of REBIN() all the time because astronomers are often interested in preserving the surface flux when resizing images. And one can do a quick test by resizing to one element.

IDL> print,rebin([2.5,3.2,3.6,9.99],1)
4.82250
IDL> print,mean([2.5,3.2,3.6,9.99])
4.82250

Perhaps you are thinking of CONGRID() which does default to nearest neighbor sampling? --Wayne
Re: Adding elements in an Array [message #85990 is a reply to message #85989] Mon, 23 September 2013 13:44 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
wlandsman writes:

> I use this property of REBIN() all the time because astronomers are often interested in preserving the surface flux when resizing images. And one can do a quick test by resizing to one element.
>
> IDL> print,rebin([2.5,3.2,3.6,9.99],1)
> 4.82250
> IDL> print,mean([2.5,3.2,3.6,9.99])
> 4.82250

I hope you document this code heavily. I don't think a casual reader
will catch this "feature" without some help. I was thinking of Congrid,
because I use that more frequently than Rebin, but "averaging when
minifying" as the default behavior! That's scary to me. :-)

Cheers,

David

--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: Adding elements in an Array [message #85991 is a reply to message #85988] Mon, 23 September 2013 13:48 Go to previous messageGo to next message
Mats Löfdahl is currently offline  Mats Löfdahl
Messages: 263
Registered: January 2012
Senior Member
On 2013-09-23 22:36, David Fanning wrote:
> David Fanning writes:
>
>>
>> wlandsman writes:
>>
>>> "Normally, REBIN uses bilinear interpolation when magnifying and neighborhood averaging when minifying."
>>
>> I suppose it would take me a couple of days scratching my head to figure
>> this out!
>>
>> Do you know this to be true, or are you just taking their word for it?
>> I'm not sure "neighborhood averaging" means what you appear to think it
>> means. If you are right, it certainly doesn't mean what I think it
>> means! And, in any case, I wouldn't bet the satellite on it until I ran
>> a couple of tests. :-)
>
> I guess you are right. Wow! I learn something every day about IDL!
>
> IDL> a = [10, 20, 5, 6, 18, 17, 1, 30, 2]
> IDL> print, rebin(a, 3)
> 11 13 11
> IDL> print, rebin(a, 3, /sample)
> 10 6 1

11 and 13 are not the mean values of [10, 20, 5] and [6, 18, 17],
respectively. 11 happens to be the mean of [1, 30, 2] but in general you
need floating point calculations:

IDL> print,rebin(float(a),3)
11.6667 13.6667 11.0000
Re: Adding elements in an Array [message #85992 is a reply to message #85991] Mon, 23 September 2013 13:51 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Mats Löfdahl writes:

> 11 and 13 are not the mean values of [10, 20, 5] and [6, 18, 17],
> respectively. 11 happens to be the mean of [1, 30, 2] but in general you
> need floating point calculations:
>
> IDL> print,rebin(float(a),3)
> 11.6667 13.6667 11.0000

Oh, I know that, but I was talking about something else entirely and
just using integer arrays to illustrate the point. :-)

Cheers,

David


--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: Adding elements in an Array [message #85993 is a reply to message #85992] Mon, 23 September 2013 13:54 Go to previous messageGo to next message
Mats Löfdahl is currently offline  Mats Löfdahl
Messages: 263
Registered: January 2012
Senior Member
On 2013-09-23 22:51, David Fanning wrote:
> Mats Löfdahl writes:
>
>> 11 and 13 are not the mean values of [10, 20, 5] and [6, 18, 17],
>> respectively. 11 happens to be the mean of [1, 30, 2] but in general you
>> need floating point calculations:
>>
>> IDL> print,rebin(float(a),3)
>> 11.6667 13.6667 11.0000
>
> Oh, I know that, but I was talking about something else entirely and
> just using integer arrays to illustrate the point. :-)

I'm sure you do. And did. But since the OP's example was with an integer
array I thought it might be pointed out. :o)
Re: Adding elements in an Array [message #85994 is a reply to message #85993] Mon, 23 September 2013 14:01 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Mats Löfdahl writes:

> I'm sure you do. And did. But since the OP's example was with an integer
> array I thought it might be pointed out. :o)

Well, that's a good point. :-)

Although the answer would have been converted to a float for him if he
did it my way, rather than Wayne's. Let's hope he didn't understand
Wayne's method any better than I did. ;-)

Cheers,

David



--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: Adding elements in an Array [message #85995 is a reply to message #85994] Mon, 23 September 2013 14:10 Go to previous messageGo to next message
Mats Löfdahl is currently offline  Mats Löfdahl
Messages: 263
Registered: January 2012
Senior Member
On 2013-09-23 23:01, David Fanning wrote:
> Mats Löfdahl writes:
>
>> I'm sure you do. And did. But since the OP's example was with an integer
>> array I thought it might be pointed out. :o)
>
> Well, that's a good point. :-)
>
> Although the answer would have been converted to a float for him if he
> did it my way, rather than Wayne's. Let's hope he didn't understand
> Wayne's method any better than I did. ;-)

I still don't understand it.

When you evaluate on a grid point, linear interpolation should give the
same result as nearest neighbor. So if the evaluation point is not the
center cell of the three here, then what is it?
Re: Adding elements in an Array [message #85996 is a reply to message #85995] Mon, 23 September 2013 14:15 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Mats Löfdahl writes:

> I still don't understand it.
>
> When you evaluate on a grid point, linear interpolation should give the
> same result as nearest neighbor. So if the evaluation point is not the
> center cell of the three here, then what is it?

Oh, man, you see where this is going, don't you? We are about to open an
ENTIRE can of worms over the very simplest question. OK, I turn you back
over to Wayne, who understands flux preservation and the workings of the
Congrid command a whole HELL of a lot better than I do. ;-)

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: Adding elements in an Array [message #85997 is a reply to message #85996] Mon, 23 September 2013 14:16 Go to previous messageGo to next message
Mats Löfdahl is currently offline  Mats Löfdahl
Messages: 263
Registered: January 2012
Senior Member
On 2013-09-23 23:15, David Fanning wrote:
> Mats Löfdahl writes:
>
>> I still don't understand it.
>>
>> When you evaluate on a grid point, linear interpolation should give the
>> same result as nearest neighbor. So if the evaluation point is not the
>> center cell of the three here, then what is it?
>
> Oh, man, you see where this is going, don't you? We are about to open an
> ENTIRE can of worms over the very simplest question. OK, I turn you back
> over to Wayne, who understands flux preservation and the workings of the
> Congrid command a whole HELL of a lot better than I do. ;-)

Except that it's not congrid... :o)
Re: Adding elements in an Array [message #85998 is a reply to message #85997] Mon, 23 September 2013 14:20 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Mats Löfdahl writes:

> Except that it's not congrid... :o)

I know, but I've been down this road before. It's going to come up.
Especially, the CENTER and MINUS_ONE keywords. My head is already
aching. :-(

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: Adding elements in an Array [message #85999 is a reply to message #85998] Mon, 23 September 2013 14:25 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
David Fanning writes:

> I know, but I've been down this road before. It's going to come up.
> Especially, the CENTER and MINUS_ONE keywords. My head is already
> aching. :-(

Just as a preemptive strike, and so I can sit most of the following
discussion out, I want you to know I *am* doing the right thing in
cgResizeImage (which I use in place of Rebin and Congrid) when I am
resizing images. I wrote this code the last time I was dragged kicking
and screaming though the discussion which is sure to follow. :-)

Cheers,

David


--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: Adding elements in an Array [message #86000 is a reply to message #85994] Mon, 23 September 2013 14:35 Go to previous messageGo to next message
wlandsman is currently offline  wlandsman
Messages: 743
Registered: June 2000
Senior Member
OK, here is why I think the behavior of REBIN is intuitive. Like all good IDL explanations it (vaguely) involves HISTOGRAM.

ReBin means to change the binning size. And, as with HISTOGRAM, when you change the binning size, the total sum over all bins must remain the same. When you REBIN a 512 x 512 array to 256 x 256 array, the total amount of "stuff" in the pixels is conserved; it is just distributed over bigger pixels. (To answer Mat's question -- REBIN does *not* do linear interpolation when minifying, it does averaging.)

This gives me a chance to plug one of my favorite programs -- FREBIN
http://idlastro.gsfc.nasa.gov/ftp/pro/image/frebin.pro
This program also conserves the "stuff" in the array, but allows new bin sizes that are not necessarily an integral fraction of the old. --Wayne
Re: Adding elements in an Array [message #86007 is a reply to message #85999] Mon, 23 September 2013 17:56 Go to previous messageGo to next message
wlandsman is currently offline  wlandsman
Messages: 743
Registered: June 2000
Senior Member
The priority for display programs when resizing images is not to conserve flux but to avoid spurious shifts and distortions. A feature in the center of the image should remain in the center of the image after resizing, and yes cgResizeImage() does this correctly.

And apologies that this thread has gotten quite far from the original question. --Wayne

On Monday, September 23, 2013 5:25:48 PM UTC-4, David Fanning wrote:
> David Fanning writes:

> Just as a preemptive strike, and so I can sit most of the following
>
> discussion out, I want you to know I *am* doing the right thing in
>
> cgResizeImage (which I use in place of Rebin and Congrid) when I am
>
> resizing images. I wrote this code the last time I was dragged kicking
>
> and screaming though the discussion which is sure to follow. :-)
>
>
>
> Cheers,
>
>
>
> David
>
>
>
>
>
> --
>
> David Fanning, Ph.D.
>
> Fanning Software Consulting, Inc.
>
> Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
>
> Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: Adding elements in an Array [message #86008 is a reply to message #86000] Mon, 23 September 2013 18:50 Go to previous message
Mats Löfdahl is currently offline  Mats Löfdahl
Messages: 263
Registered: January 2012
Senior Member
On 2013-09-23 23:35, wlandsman wrote:
> OK, here is why I think the behavior of REBIN is intuitive. Like all good IDL explanations it (vaguely) involves HISTOGRAM.
>
> ReBin means to change the binning size. And, as with HISTOGRAM, when you change the binning size, the total sum over all bins must remain the same. When you REBIN a 512 x 512 array to 256 x 256 array, the total amount of "stuff" in the pixels is conserved; it is just distributed over bigger pixels. (To answer Mat's question -- REBIN does *not* do linear interpolation when minifying, it does averaging.)

Right. I agree that the behavior is intuitive. It was just the
suggestion that it involves interpolation that I couldn't make sense of.

> This gives me a chance to plug one of my favorite programs -- FREBIN
> http://idlastro.gsfc.nasa.gov/ftp/pro/image/frebin.pro
> This program also conserves the "stuff" in the array, but allows new bin sizes that are not necessarily an integral fraction of the old. --Wayne

That's useful. Thanks!
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Google Street Maps
Next Topic: idl: turn string value into variable name part2

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

Current Time: Wed Oct 08 13:46:19 PDT 2025

Total time taken to generate the page: 0.00590 seconds