2016-06-14 75 views
0

我想在MATLAB中給一個給定的二維矩陣插入隨機數字。例如 如果我如何插入2d的隨機數字到matlab中給定的2d矩陣

A = [1 2 3; 
    4 5 6; 
    7 8 9]; 

並且如果B是被均勻地分佈矩陣的矩陣,然後我需要一個新的矩陣合併這兩個矩陣(A & B),像 新矩陣

C = [1 0.653 2 2.55 3; 
    4 4.3 5 5.4 6; 
    7 7.6 8 8.09 9] 

我怎麼能爲此編寫MATLAB代碼?

+0

請參閱:[交織矩陣在MATLAB與reshape](httt號碼://www.peteryu.ca/tutorials/matlab/interleave_matrices)。 –

回答

1

如果你已經有B並假設An -by- m矩陣和Bn -by- m-1矩陣:

[n,m] = size(A); 
C = zeros(n,2*m-1); 
C(:,1:2:end) = A; 
C(:,2:2:end) = B; % end-1 is not necessary since 2*m-1 is an odd number but if you prefer for readability then you can do C(:,2:2:end-1) = B 

您可以創建B像這樣(取決於B的限制,從你的問題不清楚)

B = A(:,1:end-1) + rand(n,m-1)*2 - 1