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

Home » Public Forums » archive » Sum along diagonals
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
Sum along diagonals [message #21460] Fri, 25 August 2000 00:00 Go to next message
mole6e23 is currently offline  mole6e23
Messages: 31
Registered: December 1998
Member
Hi there..

I thought I'd ask this question, more out of curiosity as to a possible
result than for really needing to optimize it (this code is used
approximately three times for every blue moon). I'm always impressed with
some of the results from this group, most recently:

a=a[*,where(histogram(b,MIN=0,MAX=(size(a,/DIMENSIONS))[1]-1 ,BINSIZE=1) eq 0)]

I wouldn't have thought of compacting all that into one line!

Every once in a while (not often enough to make me worry about optimizing
too much), I want to take a not necessarily square matrix and get the sum
along the diagonals, such as the following, with the theoretical function
sum_diag:

IDL> blah = indgen( 4, 4 )
IDL> print, blah
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
IDL> print, sum_diag( blah )
0 5 15 30 30 25 15

which is the series [0, 4+1, 8+5+2, 12+9+6+3, ... ]

Of course, to be difficult, I'd like it to work for non-square matrices as well:

IDL> blah = indgen( 5, 3 )
IDL> print,blah
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14

and the result would be the series [0, 5+1, 10+6+2, 11+7+3, ... ]

The best I've found is to count the diagonals, loop through those, and
then loop again through the possible row and column indices in that array.
It's pretty ugly, and very time consuming for a 512x512 array (most of
what I want). It just occured to me now that for the squre matrix, you
could take "blah + transpose( blah )" and now you only have to worry about
the lower half of the matrix (and the doubled diagonal elements), so it
might be a bit better, but still messy.

So if you're bored, I'd love to see a better solution than a bunch of
long, nested loops, even if just only for the square case (since that is
90% of the cases in my data)

Enjoy!
Todd
Re: Sum along diagonals [message #21524 is a reply to message #21460] Mon, 28 August 2000 00:00 Go to previous message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
colinr@toliman.uio.no (Colin Rosenthal) writes:

> On Fri, 25 Aug 2000 17:57:01 -0700,
> Todd Clements <mole6e23@hotmail.com> wrote:
>> craigmnet@cow.physics.wisc.edu wrote:
>>> How about this solution. It's not a one-liner, and it uses two loops,
>>> [snip]
>>
>> That works just fine! Once again, I am duly impressed. I'm sure it took me
>> longer to figure out your code than it did for you to write it! Absolutely
>> amazing. Having read this newsgroup for a while, I'm beginning to wonder
>> if there is an IDL question you can't answer.
>
> Can he write a Stein-Vidar emulator in IDL? Or maybe David could write a
> Craig emulator?

Actually Stein-Vidar is at Goddard where I work too [ in case people
didn't know. ] I made a promise long ago to meet him for lunch, but
never did. I guess now's my chance to collect data on that
emulator...

Craig

--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Origin of IDL
Next Topic: Re: IDL USERS IN ALBERTA, CANADA?

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

Current Time: Tue Dec 02 14:44:20 PST 2025

Total time taken to generate the page: 1.04444 seconds