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

Home » Public Forums » archive » Re: BEGINNER ASKS FOR HELP!!!
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: BEGINNER ASKS FOR HELP!!! [message #10943 is a reply to message #10939] Sat, 31 January 1998 00:00 Go to previous messageGo to previous message
davidf is currently offline  davidf
Messages: 2866
Registered: September 1996
Senior Member
"Edoardo Marcora" <marcora@colorado.edu> writes:

> I need to create a RANDOM square matrix that contains only three numbers 0,
> 1, and (-1). There are two constraints though. I would like to tell the
> program before it creates this matrix what the (at least approximate) RATIO
> of 1 / (-1) should be and HOW MANY (at least approximately) 1 and (-1)
> (total) are on each row of the matrix.
>
> For example, the input by the user would be:
>
> NUMBER OF ELEMENTS PER ROW e.g., 6
>
> RATIO OF 1 / (-1) e.g., 0.5 (at the level of the matrix not of the row)
>
> NUMBER OF 1 AND (-1) (total) IN EACH ROW e.g., 3 (for example 0 0 0 1 1 -1)
>
> I do not need the ratio and number of |1| per row to be exact, also an
> approximation it's good.

Uh, Lord knows I'm no statistician, but doesn't this smell
just a *wee* bit suspicious to you?

Put here you go. You can tell this little routine the ratio
of 1s to -1s, and the density of the 1/-1 distribution. For
example if I want the ratio of 1/-1 to be 0.5 and I want
40 percent (approximately) of the locations in the 10 by 10
array to be filled, I call the routine like this:

IDL> Random_Fill, 0.5, 0.40

The routine fills random locations in the array.

I can tell you that (on average) about 4 of the elements
in any particular row will be filled. (It will actually
be a little less, because my 1 and -1 locations can overlap.)
But I leave it up to you to iron out the problems. This is just
food for thought.

Cheers,

David

************************************************************ *****
PRO Random_Fill, ratio, density

; Ratio = pluses/minuses. Should be a float.
; Density = Percent of total squares occupied (e.g., 0.20)

On_Error, 1

; Check parameter values.
; Assign defaults if necessary.

CASE N_Params() OF
0: BEGIN
ratio = 1.0
density = .40
END
1: BEGIN
density = .40
END
ELSE:
ENDCASE

IF density LT 0.0 OR density GT 0.99 THEN $
Message,'Illegal density value.'
ratio = Float(ratio)
density = Fix(density * 100)

; How many 1s and -1s ?

minus = Fix(density/(ratio + 1))
plus = density - minus

; Find random positions for 1s and -1s.

arrayPlus = Floor(RandomU(seed, plus) * 100)
arrayMinus = Floor(RandomU(seed, minus) * 100)
array = IntArr(100)

; Fill array with 1s.

array(arrayPlus) = 1

; Fill array with -1s. If the cell is already full,
; flip a coin to see if a +1 or -1 should be entered.

FOR j=0,N_Elements(arrayMinus)-1 DO BEGIN
IF array(arrayMinus[j]) EQ 1 THEN BEGIN
flip = Randomu(seed, 1)
IF flip[0] LT 0.5 THEN array(arrayMinus[j]) = -1
ENDIF ELSE array(arrayMinus[j]) = -1
ENDFOR

; Reformat to a 10-by-10 array.

array = Reform(array, 10, 10)
Print, array
Print, 'Number of 1s: ', plus
Print, 'Number of -1s:', minus
a = Where(array EQ 1 OR array EQ -1, count)
Print, 'Number of non-zero values in array: ', count
END

-----------------------------------------------------------
David Fanning, Ph.D.
Fanning Software Consulting
E-Mail: davidf@dfanning.com
Phone: 970-221-0438
Coyote's Guide to IDL Programming: http://www.dfanning.com/
[Message index]
 
Read Message
Read Message
Read Message
Previous Topic: REAL DATA
Next Topic: Fun with CDF_ENCODE_EPOCH

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

Current Time: Fri Oct 10 22:01:51 PDT 2025

Total time taken to generate the page: 1.36078 seconds