Initializing matrix randomly and by sample
显示 更早的评论
Hi
I have a matrix X with size m x n and a matrix U with size m x k where k >> n.
Now first I want to fill the columns of matrix U with random columns of matrix X. How can I do that efficiently? Just make a permutation of the columns of X is not possible because there are much more columns in U than in X.
Second I want fill the columns of U with random unit length vectors. How can I do this efficiently? I think just looping over all columns in U and creating a random vector is inefficient.
采纳的回答
更多回答(2 个)
the cyclist
2014-4-29
First one:
If you have the Statistics Toolbox, you can use
U = X(:,randsample(m,k,'true'))
If not, you can use the randi() function to accomplish the same thing.
Roger Stafford
2014-4-29
For the first question use 'randi'
p = randi(n,1,k);
U = X(:,p);
For the second, very different, question do:
U = randn(m,k);
U = bsxfun(@rdivide,U,sqrt(sum(U.^2,1)));
类别
在 帮助中心 和 File Exchange 中查找有关 Random Number Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!