我想在matlab上填充一个100x100的矩阵,例如,100个1s,550个2s,700个3s,50个4s将随机定位在矩阵上。我很难做到这一点。你可以帮忙吗?
答案 0 :(得分:4)
x = [ones(1,100) 2*ones(1, 550) 3*ones(1, 700) 4*ones(1, 50)]; % desired elements
x = [x zeros(1, 100*100 - length(x))]; % zero the rest
x = x(randperm(10000)); % randomly permute
x = reshape(x, 100, 100); % reshape into matrix