Thread Subject: random selection of row and/or column from a matrix

Subject: random selection of row and/or column from a matrix

From: Dionysia Dionysiou

Date: 18 Sep, 2009 11:03:03

Message: 1 of 3

Hi,
I am trying to chose randomly one row from a specific matrix.
I have tried to generate some sample data matrix and use random indexes:
a = rand(n, m)
Then, generate a random index by using the:
index = ceil(length(a) * rand(1,1))
chosenValue = a(index) % Get the value of the array at that random index.
 
However, this will give me the index and the value of a scalar. How can I choose the index(es) of a whole row? Can I use the row part of the index?
I have tried to use the index as the 'n' (row part of the scalar), similar to:
for m=1:100; % the 'm' (column) can change subject to a 'for' loop
     chosenValue=a(index,m);
end;

However it gives an error message.
Should I define index as an array similar to:
index=ceil(length(a) * rand(1,m)
 ?
I guess it is not very difficult, but for any reason I cannot figure it out.
could you please help me on this?
Thank you in advance

Subject: random selection of row and/or column from a matrix

From: Sebastiaan

Date: 18 Sep, 2009 12:22:18

Message: 2 of 3

"Dionysia Dionysiou" <dionysia.dionysiou@stir.ac.uk> wrote in message <h8vpd7$inj$1@fred.mathworks.com>...
> Hi,
> I am trying to chose randomly one row from a specific matrix.
> I have tried to generate some sample data matrix and use random indexes:
> a = rand(n, m)
> Then, generate a random index by using the:
> index = ceil(length(a) * rand(1,1))
> chosenValue = a(index) % Get the value of the array at that random index.
>

> However, this will give me the index and the value of a scalar. How can I choose the index(es) of a whole row? Can I use the row part of the index?

You mean:
a(index, :)

?

> I have tried to use the index as the 'n' (row part of the scalar), similar to:
> for m=1:100; % the 'm' (column) can change subject to a 'for' loop
> chosenValue=a(index,m);
> end;
>
> However it gives an error message.
> Should I define index as an array similar to:
> index=ceil(length(a) * rand(1,m)
> ?
> I guess it is not very difficult, but for any reason I cannot figure it out.
> could you please help me on this?
> Thank you in advance

Subject: random selection of row and/or column from a matrix

From: Steven Lord

Date: 18 Sep, 2009 13:15:01

Message: 3 of 3


"Dionysia Dionysiou" <dionysia.dionysiou@stir.ac.uk> wrote in message
news:h8vpd7$inj$1@fred.mathworks.com...
> Hi,
> I am trying to chose randomly one row from a specific matrix.
> I have tried to generate some sample data matrix and use random indexes:
> a = rand(n, m)
> Then, generate a random index by using the:
> index = ceil(length(a) * rand(1,1))

This is not going to do what you want. If you want index to be able to
specify any element of a, then replace length(a) with numel(a). If you want
index to be able to specify any row of a, replace length(a) with either n or
size(a, 1). Read the help for LENGTH very carefully (paying particular
attention to what it does for matrices) to understand why it's not the right
tool for this instance.

> chosenValue = a(index) % Get the value of the array at that random index.
>
> However, this will give me the index and the value of a scalar. How can I
> choose the index(es) of a whole row? Can I use the row part of the index?
> I have tried to use the index as the 'n' (row part of the scalar), similar
> to:
> for m=1:100; % the 'm' (column) can change subject to a 'for' loop
> chosenValue=a(index,m);
> end;
>
> However it gives an error message.
> Should I define index as an array similar to:
> index=ceil(length(a) * rand(1,m)

rowIndex = ceil(size(a, 1)*rand(1));
chosenRows = a(rowIndex, :);

If you're using a version of MATLAB that includes the RANDI function, I'd
use that instead of ceil(c*rand).

rowIndex = randi(size(a, 1), 1, 1);
chosenRows = a(rowIndex, :);

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

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
random row in m... Sprinceana 18 Sep, 2009 11:03:34
matrix Sprinceana 18 Sep, 2009 11:03:34
random Sprinceana 18 Sep, 2009 11:03:34
rssFeed for this Thread

Contact us at files@mathworks.com