how to generate vector by selecting from another vector?

5 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Image Analyst
Image Analyst am 6 Aug. 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.

Weitere Antworten (1)

Azzi Abdelmalek
Azzi Abdelmalek am 6 Aug. 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)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by