I wanted to know how I can go about randomly placing numbers (up to 10 numbers) in a matrix. The numbers will range from 1 to 10.
I am starting with A = zeros(5,8)
, then randomly place the 10 random numbers around the matrix.
Example of matrix:
I wanted to know how I can go about randomly placing numbers (up to 10 numbers) in a matrix. The numbers will range from 1 to 10.
I am starting with A = zeros(5,8)
, then randomly place the 10 random numbers around the matrix.
Example of matrix:
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
Basically you use randperm
to create a certain number of linear indices to index your matrix. Simply place the desired numbers there and you're done.