Initializing matrix randomly and by sample
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Sepp
am 29 Apr. 2014
Beantwortet: Roger Stafford
am 29 Apr. 2014
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.
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 29 Apr. 2014
Try this:
% Create sample data.
x = randi(4, [3,4]);
u = zeros([3,20]);
% Get the two sizes.
[rowsx, colsx] = size(x)
[rowsu, colsu] = size(u)
% Get list of random columns of x to transfer to u.
xColsToUse = randi(colsx, [colsu, 1])
% Transfer columns of x to u
u(:, 1:colsu) = x(:, xColsToUse)
0 Kommentare
Weitere Antworten (2)
the cyclist
am 29 Apr. 2014
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.
0 Kommentare
Roger Stafford
am 29 Apr. 2014
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)));
0 Kommentare
Siehe auch
Kategorien
Mehr zu Descriptive Statistics 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!