2012-09-03 75 views

回答

2

一個非常直接的方法是使用for循環。它可能不是MATLAB中最有效的方法,但考慮到您的數據集太小,它可能已經足夠了。

遍歷四列中的每一列。在每次迭代中,隨機選擇一個從1到7的數字來表示該列中您選擇要更改的行。最後,翻轉該行/列的位。下面的代碼就是這樣做的。假設「A」是7行4列

for col=1:4;     %// Iterate through each column 
    row = ceil(7*rand());  %// Randomly chose a number from 1 to 7 to represent row 
    A(row,col) = ~A(row,col); %// Flip the bit at the specified row/col 
end 
+1

'row = randi(7)'在這個答案中是第二行代碼的簡單版本 –

+0

好點。我完全忘記了'蘭迪'。 – grungetta

1

另一種可能性是在一個呼叫建立4張隨機數,並以量化的方式分配二元矩陣:

rowNumbers = randi(4,[1 4]) 
A(rowNumbers,:) = ~A(rowNumbers,:);