how to generate vector by selecting from another vector?

There is a matrix, "a" that is n by 1. I need to generate another matrix with this size (n by r), by selecting randomly from "a". I need a function like unifrnd(min, max, [d f]), but selecting from another matrix. Would you please guide me in this regard?
Thanks in advance,
Mina

 Réponse acceptée

Mina, try this:
% Create some sample input variable "a".
n = 1000; % Whatever you want...
% Create sample data, or use some existing vector if you want
a = 1 : n;
% Now we have sample data and we can begin.
r = 10; % Whatever you want...
% Need an n-by-r output matrix,
% so we need to pull n*r elements
% from "a" at random locations.
indexes = randi(length(a), 1, n*r);
% Pull those indexes out and reshape them
% from a 1-D vector into an n row-by-r column matrix.
out = reshape(a(indexes), n, r);
It's well commented so hopefully you can follow it but if you can't just ask.

Plus de réponses (1)

%-----------Example-------------
n=10
r=4
A=randi(10,1,n)
%------The code----------
AA=sort(A)
a = AA(1);
bb=AA(2:end)
b=repmat(bb,r,1)
out = unifrnd(a,b)

Catégories

En savoir plus sur Random Number Generation dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by