Создание случайной матрицы с 10%, .., 90% нулями - PullRequest
0 голосов
/ 27 января 2019

Я пытаюсь создать матрицу с записями от -1 до 1 и 10%, 20%, ..., 90% нулей в matlab.Пока что у меня есть

numRows = 100;                        % Number of Rows
numCols = 2*numRows;                  % Number of Columns
percentageZeros = 0.1 : 0.1 : 0.9;    % Percentage of Zeros (options)


for currPercentageZeros = percentageZeros

  do 
    A = zeros(numRows, numCols);   % Initialize matrix

    for row = 1:numRows
      for col = 1:numCols
        rnd = rand;
        if rnd > currPercentageZeros
          A(row, col) = 2*rand-1;
        endif  
      endfor
    endfor
  until (rank(A) == numRows)

, но это не совсем точно.Кто-нибудь знает лучшее решение?

1 Ответ

0 голосов
/ 27 января 2019

Предполагая, что вы хотите, чтобы остаток массива был равномерно распределен между -1 и 1

numRows = 100;
numCols = 2*numRows;
percentageZeros = 0.1 : 0.1 : 0.9;

for i = 1:length(percentageZeros)
  r = rand(numRows, numCols); % create an array of random floats between 0 and 1
  A = 2 * rand(numRows, numCols) - 1; % create your output array between -1 and 1
  A(r <= percentageZeros(i)) = 0; % use r to decide which elements to remove from A
end
...