|
"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
|