matrix division [message #60444] |
Wed, 21 May 2008 11:29  |
adam.ranson
Messages: 4 Registered: May 2008
|
Junior Member |
|
|
Hi,
I have a question about IDL 5.6 which I'm pretty new to... I seem to
remember having a similar problem in MatLab a while back too. I'm
trying to divide each of the elements in a 2D array by the elements in
2nd 2D array of identical dimensions and put the result into 3rd
array. Is it possible to do this in one statement in IDL or do I have
to go through each item using a loop?
Many thanks for any help anyone can offer!
Adam
|
|
|
Re: matrix division [message #60537 is a reply to message #60444] |
Fri, 23 May 2008 13:41  |
Kenneth P. Bowman
Messages: 585 Registered: May 2000
|
Senior Member |
|
|
In article
<27c2f921-f4ce-4b62-96e5-9a9507dc378c@w1g2000prd.googlegroups.com>,
"edward.s.meinel@aero.org" <meinel@aero.org> wrote:
> On May 22, 4:15 pm, "Kenneth P. Bowman" <k-bow...@null.edu> wrote:
>>
>> All of these variations are clever, and I can see why you might
>> want to do this in special circumstances, but usually when I
>> divide by zero, I want to know about. A floating-point exception
>> and Inf work for me.
>>
>> Cheers, Ken
>
> Exactamundo, Ken. This provides the user with a choice -- if the user
> wants to know about the zero-divide, just do c=a/b; if there are known
> special cases, use the clever solution.
Right, people should not assume that this is the right thing
to do in every case. I think in most cases users really want
to generate a floating-point exception when they divide by
zero.
Ken
|
|
|
|
Re: matrix division [message #60540 is a reply to message #60444] |
Fri, 23 May 2008 11:02  |
Vince Hradil
Messages: 574 Registered: December 1999
|
Senior Member |
|
|
On May 23, 12:23 pm, Jean H <jghas...@DELTHIS.ucalgary.ANDTHIS.ca>
wrote:
> Vince Hradil wrote:
>> On May 23, 11:41 am, Jean H <jghas...@DELTHIS.ucalgary.ANDTHIS.ca>
>> wrote:
>>>> > c = a/(b + replacement_value*(abs(b) lt epsilon))
>>>> Ooooo, good one, Jean.
>>> on the other hand, doing this, when 'b' is 0, would lead to 'c = a /
>>> replacement_value'... so in 'c', you would get different results as 'a'
>>> varies..
>
>>> Jean
>
>> Ummm... isn't that the idea?
>
> or is it to flag the bad entries? ... though having "inf" would do so
> very well:) ... nevermind then (that Friday rain is playing on my brain.. :)
>
> Jean
I see what you mean. Yes, there are two issues here - do you want to
(quietly) avoid the divide-by-zero or do you want to flag them. Two
issues and two different solutions.
|
|
|
Re: matrix division [message #60541 is a reply to message #60444] |
Fri, 23 May 2008 10:23  |
Jean H.
Messages: 472 Registered: July 2006
|
Senior Member |
|
|
Vince Hradil wrote:
> On May 23, 11:41 am, Jean H <jghas...@DELTHIS.ucalgary.ANDTHIS.ca>
> wrote:
>>>> c = a/(b + replacement_value*(abs(b) lt epsilon))
>>> Ooooo, good one, Jean.
>> on the other hand, doing this, when 'b' is 0, would lead to 'c = a /
>> replacement_value'... so in 'c', you would get different results as 'a'
>> varies..
>>
>> Jean
>
> Ummm... isn't that the idea?
or is it to flag the bad entries? ... though having "inf" would do so
very well:) ... nevermind then (that Friday rain is playing on my brain.. :)
Jean
|
|
|
Re: matrix division [message #60542 is a reply to message #60444] |
Fri, 23 May 2008 10:06  |
Vince Hradil
Messages: 574 Registered: December 1999
|
Senior Member |
|
|
On May 23, 11:41 am, Jean H <jghas...@DELTHIS.ucalgary.ANDTHIS.ca>
wrote:
>>> c = a/(b + replacement_value*(abs(b) lt epsilon))
>
>> Ooooo, good one, Jean.
>
> on the other hand, doing this, when 'b' is 0, would lead to 'c = a /
> replacement_value'... so in 'c', you would get different results as 'a'
> varies..
>
> Jean
Ummm... isn't that the idea?
|
|
|
Re: matrix division [message #60543 is a reply to message #60444] |
Fri, 23 May 2008 09:41  |
Jean H.
Messages: 472 Registered: July 2006
|
Senior Member |
|
|
>> c = a/(b + replacement_value*(abs(b) lt epsilon))
>
> Ooooo, good one, Jean.
on the other hand, doing this, when 'b' is 0, would lead to 'c = a /
replacement_value'... so in 'c', you would get different results as 'a'
varies..
Jean
|
|
|
Re: matrix division [message #60544 is a reply to message #60444] |
Fri, 23 May 2008 08:13  |
edward.s.meinel@aero.
Messages: 52 Registered: February 2005
|
Member |
|
|
On May 22, 4:15 pm, "Kenneth P. Bowman" <k-bow...@null.edu> wrote:
>
> All of these variations are clever, and I can see why you might
> want to do this in special circumstances, but usually when I
> divide by zero, I want to know about. A floating-point exception
> and Inf work for me.
>
> Cheers, Ken
Exactamundo, Ken. This provides the user with a choice -- if the user
wants to know about the zero-divide, just do c=a/b; if there are known
special cases, use the clever solution.
In article <g14hfu$fl...@news.ucalgary.ca>, Jean H
<jghas...@DELTHIS.ucalgary.ANDTHIS.ca> wrote:
> c = a/(b + replacement_value*(abs(b) lt epsilon))
Ooooo, good one, Jean.
David Fanning <n...@dfanning.com> wrote:
> I've been trying to present a more professional demeanor. :-(
Noooooooooo!!!
Ed
P.S. So, David, does that mean that de older you are, demeanor you
get?
|
|
|
|
Re: matrix division [message #60555 is a reply to message #60444] |
Thu, 22 May 2008 13:15  |
Kenneth P. Bowman
Messages: 585 Registered: May 2000
|
Senior Member |
|
|
In article <g14hfu$fl3$1@news.ucalgary.ca>,
Jean H <jghasban@DELTHIS.ucalgary.ANDTHIS.ca> wrote:
>>> c = a/(b + replacement_value*(b EQ 0))
>>
>> Yeah, I'm *definitely* writing this down, right now!
>>
>> But, what about floats? :-)
>
>
> c = a/(b + replacement_value*(abs(b) lt epsilon))
>
> Jean
All of these variations are clever, and I can see why you might
want to do this in special circumstances, but usually when I
divide by zero, I want to know about. A floating-point exception
and Inf work for me.
Cheers, Ken
|
|
|
Re: matrix division [message #60559 is a reply to message #60444] |
Thu, 22 May 2008 12:28  |
Jean H.
Messages: 472 Registered: July 2006
|
Senior Member |
|
|
>> c = a/(b + replacement_value*(b EQ 0))
>
> Yeah, I'm *definitely* writing this down, right now!
>
> But, what about floats? :-)
c = a/(b + replacement_value*(abs(b) lt epsilon))
Jean
|
|
|
Re: matrix division [message #60562 is a reply to message #60444] |
Thu, 22 May 2008 10:45  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
edward.s.meinel@aero.org writes:
> David, David, David...
>
> This will be about the fourth or fifth time I've posted the elegant
> solution. Put it on your site already!
>
> c = a/(b + replacement_value*(b EQ 0))
Yeah, I'm *definitely* writing this down, right now!
But, what about floats? :-)
> PPS: David, why no more PS-ing in your posts? I miss them.
I've been trying to present a more professional demeanor. :-(
Cheers,
David
P.S. Let's just say, with the IDL Workbench and further
set-backs in athletic ability due to old age, my cynicism
is getting out of control. :-(
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming (www.dfanning.com)
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|
Re: matrix division [message #60563 is a reply to message #60444] |
Thu, 22 May 2008 10:22  |
Rich Younger
Messages: 3 Registered: January 2008
|
Junior Member |
|
|
Rich Younger wrote:
> edward.s.meinel@aero.org wrote:
>> On May 22, 11:48 am, David Fanning <n...@dfanning.com> wrote:
>>> Shoot. I was hoping for an elegant solution. :-(
>>>
>>> Cheers,
>>>
>>> David
>>>
>> David, David, David...
>>
>> This will be about the fourth or fifth time I've posted the elegant
>> solution. Put it on your site already!
>>
>> c = a/(b + replacement_value*(b EQ 0))
>>
>> Ed
>>
>> PS: That would be soooooo cool to be referenced by Coyote!
>>
>> PPS: David, why no more PS-ing in your posts? I miss them.
>
> c = a/(b EQ 0 ? replacement_value : b) doesn't require an extra
> multiplication.
>
> Rich
... but isn't valid for vectors.
Never mind.
Rich
|
|
|
Re: matrix division [message #60564 is a reply to message #60444] |
Thu, 22 May 2008 10:13  |
Rich Younger
Messages: 3 Registered: January 2008
|
Junior Member |
|
|
edward.s.meinel@aero.org wrote:
> On May 22, 11:48 am, David Fanning <n...@dfanning.com> wrote:
>> Shoot. I was hoping for an elegant solution. :-(
>>
>> Cheers,
>>
>> David
>>
> David, David, David...
>
> This will be about the fourth or fifth time I've posted the elegant
> solution. Put it on your site already!
>
> c = a/(b + replacement_value*(b EQ 0))
>
> Ed
>
> PS: That would be soooooo cool to be referenced by Coyote!
>
> PPS: David, why no more PS-ing in your posts? I miss them.
c = a/(b EQ 0 ? replacement_value : b) doesn't require an extra
multiplication.
Rich
|
|
|
Re: matrix division [message #60565 is a reply to message #60444] |
Thu, 22 May 2008 09:47  |
edward.s.meinel@aero.
Messages: 52 Registered: February 2005
|
Member |
|
|
On May 22, 11:48 am, David Fanning <n...@dfanning.com> wrote:
>
> Shoot. I was hoping for an elegant solution. :-(
>
> Cheers,
>
> David
>
David, David, David...
This will be about the fourth or fifth time I've posted the elegant
solution. Put it on your site already!
c = a/(b + replacement_value*(b EQ 0))
Ed
PS: That would be soooooo cool to be referenced by Coyote!
PPS: David, why no more PS-ing in your posts? I miss them.
|
|
|
Re: matrix division [message #60566 is a reply to message #60444] |
Thu, 22 May 2008 09:19  |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
David Fanning wrote:
> jameskuyper@verizon.net writes:
>
>> For the integer case:
>>
>> ratio = numerator/denominator
>> bad = WHERE(denominator eq 0, count)
>> IF count gt 0 THEN ratio[bad] = replacement_value
>>
>> replacement_value needs to be carefully chose for the context of your
>> problem. You might want to give different replacement values depending
>> upon whether the numerator is positive, negative, or 0. There's
>> probably a more elegant approach, but three different WHERE's would
>> certainly be sufficient to cover those cases.
>>
>> For the floating point case, IDL fills in the relevant elements of
>> ratio with -Inf, Nan, or Inf, depending upon the sign of the
>> numerator, and I can't think of any better way of handling it than to
>> use precisely those value. If you need to use your result for further
>> computations where those values would be unacceptable, replace them,
>> with logic similar to the above, but using the FINITE() function.
>
> Shoot. I was hoping for an elegant solution. :-(
For something like this, the Fortran WHERE construct is teeny bit more elegant, e.g.
real, dimension(100,100) :: ratio, numerator, denominator
....
where (abs(denominator) > 0.0)
ratio = numerator/denominator
elsewhere
ratio = -999.9
end where
I like the fact that I don't have to explicitly use any indices indicated where good or
bad values are.
Anyway....
cheers,
paulv
|
|
|
Re: matrix division [message #60567 is a reply to message #60444] |
Thu, 22 May 2008 08:48  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
jameskuyper@verizon.net writes:
> For the integer case:
>
> ratio = numerator/denominator
> bad = WHERE(denominator eq 0, count)
> IF count gt 0 THEN ratio[bad] = replacement_value
>
> replacement_value needs to be carefully chose for the context of your
> problem. You might want to give different replacement values depending
> upon whether the numerator is positive, negative, or 0. There's
> probably a more elegant approach, but three different WHERE's would
> certainly be sufficient to cover those cases.
>
> For the floating point case, IDL fills in the relevant elements of
> ratio with -Inf, Nan, or Inf, depending upon the sign of the
> numerator, and I can't think of any better way of handling it than to
> use precisely those value. If you need to use your result for further
> computations where those values would be unacceptable, replace them,
> with logic similar to the above, but using the FINITE() function.
Shoot. I was hoping for an elegant solution. :-(
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming (www.dfanning.com)
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|