Thread Subject:
Selecting a "random" number based on second row of matrix and other criteria

Subject: Selecting a "random" number based on second row of matrix and other criteria

From: Kenny

Date: 13 Oct, 2012 21:15:08

Message: 1 of 5

I have something like the following:

A = [1 2 3 4 5 6; 9 5 7 1 2 6; 6 2 3 1 4 5];

Max = 8;

I am looking to be able to choose a "random" number that is contained in the second column. Therefore the number can be 9 5 7 1 2 or 6 and also be less than the Max = 8;

So, a number to be chosen that is in the list of 1 2 5 6 7 (for this case).

Is this feasible?

Subject: Selecting a "random" number based on second row of matrix and

From: Nasser M. Abbasi

Date: 13 Oct, 2012 21:23:06

Message: 2 of 5

On 10/13/2012 4:15 PM, Kenny wrote:
> I have something like the following:
>
> A = [1 2 3 4 5 6;
        9 5 7 1 2 6;
        6 2 3 1 4 5];
>
> Max = 8;
>
> I am looking to be able to choose a "random" number that is contained in the second column.
>Therefore the number can be 9 5 7 1 2 or 6 and also be less than the Max = 8;
>

How can the number be '9' and also be less than Max=8 at the same time? btw, the
second column has {2,5,6} numbers in it. What you typed above is the second
row?

> So, a number to be chosen that is in the list of 1 2 5 6 7 (for this case).
>
> Is this feasible?
>

to select a random number from a vector, one way

------------------------------
EDU>> v=[1 2 3 4 5 6];
EDU>> v(randi(length(v)))
------------------------------

or

------------------------------
EDU>> v=[1 2 3 4 5 6];
EDU>> r=randperm(length(v));
EDU>> v(r(1))
-------------------------------

so for your case, your the second column (or the second row) in place
of v above.

--Nasser

Subject: Selecting a "random" number based on second row of matrix and

From: Kenny

Date: 13 Oct, 2012 21:42:05

Message: 3 of 5

"Nasser M. Abbasi" <nma@12000.org> wrote in message <k5cm3v$ga$1@speranza.aioe.org>...
> On 10/13/2012 4:15 PM, Kenny wrote:
> > I have something like the following:
> >
> > A = [1 2 3 4 5 6;
> 9 5 7 1 2 6;
> 6 2 3 1 4 5];
> >
> > Max = 8;
> >
> > I am looking to be able to choose a "random" number that is contained in the second column.
> >Therefore the number can be 9 5 7 1 2 or 6 and also be less than the Max = 8;
> >
>
> How can the number be '9' and also be less than Max=8 at the same time? btw, the
> second column has {2,5,6} numbers in it. What you typed above is the second
> row?
>
> > So, a number to be chosen that is in the list of 1 2 5 6 7 (for this case).
> >
> > Is this feasible?
> >
>
> to select a random number from a vector, one way
>
> ------------------------------
> EDU>> v=[1 2 3 4 5 6];
> EDU>> v(randi(length(v)))
> ------------------------------
>
> or
>
> ------------------------------
> EDU>> v=[1 2 3 4 5 6];
> EDU>> r=randperm(length(v));
> EDU>> v(r(1))
> -------------------------------
>
> so for your case, your the second column (or the second row) in place
> of v above.
>
> --Nasser
>
>
>
>
>
Great, thanks! Would it be possible to integrate the Max, so the random number is less than or equal to the Max, in addition to being one of those numbers?

For your example:

clear all;

 v=[1 2 3 4 5 6];
 v
 Max = 5;
 
 g=v(randi(length(v)));
 g

basically adding to it that the random number cannot be greater than the Max.

Subject: Selecting a "random" number based on second row of matrix and

From: Nasser M. Abbasi

Date: 13 Oct, 2012 22:23:00

Message: 4 of 5

On 10/13/2012 4:42 PM, Kenny wrote:

> Great, thanks! Would it be possible to integrate the Max, so the
> random number is less than or equal to the Max, in addition to being one of those numbers?
>
> For your example:
>
> clear all;
>
> v=[1 2 3 4 5 6];
> v
> Max = 5;
>
> g=v(randi(length(v)));
> g
>
> basically adding to it that the random number cannot be greater than the Max.
>

sure. Simply filter out the data you do not want first, then apply
the same logic.

To filter out everything below max, use find() then apply randi() to
the resulting vector.

-----------------
v = [1 2 3 4 5 6 7 8];
myMax = 5;
tmp = v(v<myMax)
tmp(randi(length(tmp)))
-----------------------

Subject: Selecting a "random" number based on second row of matrix and

From: Kenny

Date: 13 Oct, 2012 22:33:08

Message: 5 of 5

"Nasser M. Abbasi" <nma@12000.org> wrote in message <k5cpkd$8h6$1@speranza.aioe.org>...
> On 10/13/2012 4:42 PM, Kenny wrote:
>
> > Great, thanks! Would it be possible to integrate the Max, so the
> > random number is less than or equal to the Max, in addition to being one of those numbers?
> >
> > For your example:
> >
> > clear all;
> >
> > v=[1 2 3 4 5 6];
> > v
> > Max = 5;
> >
> > g=v(randi(length(v)));
> > g
> >
> > basically adding to it that the random number cannot be greater than the Max.
> >
>
> sure. Simply filter out the data you do not want first, then apply
> the same logic.
>
> To filter out everything below max, use find() then apply randi() to
> the resulting vector.
>
> -----------------
> v = [1 2 3 4 5 6 7 8];
> myMax = 5;
> tmp = v(v<myMax)
> tmp(randi(length(tmp)))
> -----------------------
>
Thank you. I was thinking in the reverse -- filtering out the data first makes sense.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
matlab random matr... Kenny 13 Oct, 2012 17:19:12
rssFeed for this Thread

Contact us