how to generate vector by selecting from another vector?

10 vues (au cours des 30 derniers jours)
mina movahed
mina movahed le 6 Août 2016
Commenté : mina movahed le 8 Août 2016
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

Image Analyst
Image Analyst le 6 Août 2016
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)

Azzi Abdelmalek
Azzi Abdelmalek le 6 Août 2016
%-----------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)
  1 commentaire
mina movahed
mina movahed le 6 Août 2016
Thanks for you response, I will try this.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by