答案 0 :(得分:3)
N=20; %// number of columns
M=1024; %// number of rows
NumRand = 20; %// number of random numbers
RandomScalars = rand(NumRand,1); %// random numbers
MyMatrix= sparse(M,N); %// initialise matrix
Idx = randperm(M*N,NumRand); %// get indices to be filled
MyMatrix(Idx) = RandomScalars; %// fill indexed places
基本上,您使用randperm
创建一定数量的linear indices来索引矩阵。只需将所需的数字放在那里就可以了。