help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Generation of random number which needs to be passed to an array


From: Doug Stewart
Subject: Re: Generation of random number which needs to be passed to an array
Date: Tue, 16 Jun 2020 10:47:36 -0400



On Mon, Jun 15, 2020 at 10:42 PM Nicholas Jankowski <jankowskin@asme.org> wrote:
On Mon, Jun 15, 2020 at 6:25 PM Ganesh Kini <ganeshrkini19@gmail.com> wrote:
No sir, It is not. 

I can pass it via for loop but I don't want to take that approach.

Kindly let me know

as before, please post after the quoted reply, not before it. it makes it easier for readers to follow the conversation.

I assume you didn't actually mean 'randomly', but you are looking for all of the combinations. 

What you are doing looks like the generation of a 2d coordinate mesh, given vectors A and B.  You can use the meshgrid function to make all of those combinations.

>> A = 1:0.2:2;B = 10:0.2:11;[aa, bb] = meshgrid (A,B)
aa =

   1.0000   1.2000   1.4000   1.6000   1.8000   2.0000
   1.0000   1.2000   1.4000   1.6000   1.8000   2.0000
   1.0000   1.2000   1.4000   1.6000   1.8000   2.0000
   1.0000   1.2000   1.4000   1.6000   1.8000   2.0000
   1.0000   1.2000   1.4000   1.6000   1.8000   2.0000
   1.0000   1.2000   1.4000   1.6000   1.8000   2.0000

bb =

   10.000   10.000   10.000   10.000   10.000   10.000
   10.200   10.200   10.200   10.200   10.200   10.200
   10.400   10.400   10.400   10.400   10.400   10.400
   10.600   10.600   10.600   10.600   10.600   10.600
   10.800   10.800   10.800   10.800   10.800   10.800
   11.000   11.000   11.000   11.000   11.000   11.000


Now, the positions of aa and bb give you every combination of A, B.

if you want this reshaped as a single array, you could use:

>> C = [aa(:), bb(:)]
C =

    1.0000   10.0000
    1.0000   10.2000
    1.0000   10.4000
    1.0000   10.6000
    1.0000   10.8000
    1.0000   11.0000
    1.2000   10.0000
    1.2000   10.2000
    1.2000   10.4000
    1.2000   10.6000
    1.2000   10.8000
    1.2000   11.0000
    1.4000   10.0000
    1.4000   10.2000
    1.4000   10.4000
    1.4000   10.6000
    1.4000   10.8000
    1.4000   11.0000
    1.6000   10.0000
    1.6000   10.2000
    1.6000   10.4000
    1.6000   10.6000
    1.6000   10.8000
    1.6000   11.0000
    1.8000   10.0000
    1.8000   10.2000
    1.8000   10.4000
    1.8000   10.6000
    1.8000   10.8000
    1.8000   11.0000
    2.0000   10.0000
    2.0000   10.2000
    2.0000   10.4000
    2.0000   10.6000
    2.0000   10.8000
    2.0000   11.0000

Or however else you want to manipulate the array to make it useful for you.

Great answer Nicholas!
Ganesh:  does this do what you want?

Do you still want any randomness ? 


--
DASCertificate for 206392


reply via email to

[Prev in Thread] Current Thread [Next in Thread]