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

Home » Public Forums » archive » Easy question?
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
Easy question? [message #16325] Wed, 14 July 1999 00:00 Go to next message
colinr is currently offline  colinr
Messages: 30
Registered: July 1999
Member
If I have an i*j*k array and vector of length k and I want to
put the vector in every column of the array how do I do it?

More specifically, I have a 3-d array and wish to compute the average
value of the array over two of the dimensions and store the result in
a 3-d array of the same size as the original.

--
Colin Rosenthal
Astrophysics Institute
University of Oslo
Re: easy question [message #45749 is a reply to message #16325] Fri, 07 October 2005 12:58 Go to previous message
Richard French is currently offline  Richard French
Messages: 173
Registered: December 2000
Senior Member
You can also dispense with the begin and endif/endelse entries as follows:



pro mydemo,arg

if arg eq 1 then print,"one" $
else if arg eq 2 then print,"two" $
else print, "many
end

mydemo,1
mydemo,2
mydemo,3
End


On 10/6/05 10:48 PM, in article di4nlo$2550$1@agate.berkeley.edu, "Marshall
Perrin" <mperrin+news@cymric.berkeley.edu> wrote:

>
> Both Michael and Haje are correct that one can use a case statement
> for this sort of thing. But IDL handles a series of if statements just
> fine, too:
>
> pro demo, arg
>
> if arg eq 1 then begin
> print,"one"
> endif else if arg eq 2 then begin
> print,"two"
> endif else begin
> print, "many"
> endelse
>
> end
>
> This requires typing a lot more characters than the case version,
> but I think it can sometimes make the program logic a bit clearer.
> Either way works, so feel free to use whichever idiom you feel more
> comfortable with.
>
> - Marshall
>
>
Re: easy question [message #45753 is a reply to message #16325] Thu, 06 October 2005 19:48 Go to previous message
mperrin+news is currently offline  mperrin+news
Messages: 81
Registered: May 2001
Member
Both Michael and Haje are correct that one can use a case statement
for this sort of thing. But IDL handles a series of if statements just
fine, too:

pro demo, arg

if arg eq 1 then begin
print,"one"
endif else if arg eq 2 then begin
print,"two"
endif else begin
print, "many"
endelse

end

This requires typing a lot more characters than the case version,
but I think it can sometimes make the program logic a bit clearer.
Either way works, so feel free to use whichever idiom you feel more
comfortable with.

- Marshall
Re: easy question [message #45754 is a reply to message #16325] Thu, 06 October 2005 14:55 Go to previous message
ChiChiRuiz@gmail.com is currently offline  ChiChiRuiz@gmail.com
Messages: 32
Registered: October 2005
Member
thanks a bunch!
Re: easy question [message #45755 is a reply to message #16325] Thu, 06 October 2005 11:21 Go to previous message
Haje Korth is currently offline  Haje Korth
Messages: 651
Registered: May 1997
Senior Member
use "case 1of"

<ChiChiRuiz@gmail.com> wrote in message
news:1128621045.617508.124150@g47g2000cwa.googlegroups.com.. .
> I'm pretty new to IDL. I've learned that the if-else statement goes
> like this:
> if (expression) then begin
> ...
> endif else begin
> ...
> endelse
>
> I was wondering what if IDL supports the following (as in Matlab):
> if (expression1) then ...
> else if (expression2) then...
> else if (expression3) then...
> ...
>
> endif
>
> What's an alternative approach for it? Or I'll need to write a nested
> if-else statement?
> thanks for your help.
>
Re: easy question [message #45756 is a reply to message #16325] Thu, 06 October 2005 11:27 Go to previous message
Michael Wallace is currently offline  Michael Wallace
Messages: 409
Registered: December 2003
Senior Member
You can use a switch or case statement in this case. You can do
something like the following

case 1 of
(x eq 1): print, 'Test 1'
(x eq 2): print, 'Test 2'
(x eq 3): print, 'Test 3'
(x eq 4): print, 'Test 4'
endcase

I'm looking for the first expression that evaluates to true (1). Each
of the expressions are evaluated in turn and the statement associated
with the first expression that evaluates to true is run. If this same
logic were written in a language such as C, it would look like this:

if (x == 1) printf("Test 1\n")
else if (x == 2) printf("Test 2\n")
else if (x == 3) printf("Test 3\n")
else if (x == 4) printf("Test 4\n");

The case construct I used at the top doesn't exist in C or other
languages because there's usually a requirement that the values in the
case or switch must be constants rather than expressions.

Hope that helps,
-Mike
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Five days mean values
Next Topic: Re: how to use map_set in object graphics programming

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

Current Time: Wed Oct 08 15:08:06 PDT 2025

Total time taken to generate the page: 0.07963 seconds