MATLAB: Random Binary Matrix with certain weight

MATLABrandom number generator

In leaner codes (n,k), I want to generate a random binary matrix of size (1, n) with weight (t)
I can use A = randi ([0 1], 1, n); OR A=fix(2*rand(1,n));
But this will generate a matrix A of different weights. To get a vector of weight (t), will need exhaustive search which is time-consuming.
I need a command that can generate this matrix or vector of weight (t) distributed randomly in A=(1,n).
Can you help pls?

Best Answer

v = [ones(1,t), zeros(1,n-t)];
A = v( randperm(n) );