2016-03-20 137 views
2

如果我有矩陣size(mat)= X*Y*6如何調整MATLAB矩陣

讓調用mat(:,:,1)=A

mat(:,:,2)=B

我怎麼rezise墊X * Y * 12

其中

mat(:,:,1)=mat(:,:,2)= A 
mat(:,:,3)=mat(:,:,4)=B 

回答

1

您可以使用下面的語法:

%defines input matrix (in your case it is already defined) 
m = 500; 
n = 400; 
z = 6; 
mat = rand(m,n,z); 

%initialize output matrix 
newMat = zeros(m,n,z*2); 

%assign old matrix values into the new matrix 
newMat(:,:,1:2:end) = mat; 
newMat(:,:,2:2:end) = mat; 
+0

在最後兩行中,您是不是指:'newMat(:,:,1:2:end)= mat'? – Suever

+0

@Suever - 絕對 - 這就是我的意思:)謝謝你的評論! – drorco

1

如果你有Matlab的2015A或更新您可以使用repelem

N = 2; %// number of times to repeat 
result = repelem(mat, 1, 1, N); %// repeat N times along 3rd dim 

對於較舊的Matlab的版本,你可以做手工如下:

N = 2; %// number of times to repeat 
ind = ceil(1/N:1/N:size(mat,3)); %// build index with repetitions 
result = mat(:,:,ind); %// apply index along desired dim 

例如:

>> %// Data 
>> mat = randi(9,2,4,2) 
mat(:,:,1) = 
    5  8  9  2 
    7  3  1  5 
mat(:,:,2) = 
    5  7  1  1 
    1  8  8  2 

>> %// First approach 
>> N = 2; 
>> result = repelem(mat, 1, 1, N) 
result(:,:,1) = 
    5  8  9  2 
    7  3  1  5 
result(:,:,2) = 
    5  8  9  2 
    7  3  1  5 
result(:,:,3) = 
    5  7  1  1 
    1  8  8  2 
result(:,:,4) = 
    5  7  1  1 
    1  8  8  2 

>> %// Second approach 
>> N = 2; 
>> ind = ceil(1/N:1/N:size(mat,3)); 
>> result = mat(:,:,ind) 
result(:,:,1) = 
    5  8  9  2 
    7  3  1  5 
result(:,:,2) = 
    5  8  9  2 
    7  3  1  5 
result(:,:,3) = 
    5  7  1  1 
    1  8  8  2 
result(:,:,4) = 
    5  7  1  1 
    1  8  8  2