how to generate vector by selecting from another vector?

4 次查看(过去 30 天)
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

采纳的回答

Image Analyst
Image Analyst 2016-8-6
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.

更多回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2016-8-6
%-----------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)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by