how to generate vector by selecting from another vector?
3 ビュー (過去 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
0 件のコメント
採用された回答
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
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 Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!