Reputation: 55
Let's say I have a 3 x 3 matrix (A), and I want to make it a 5 x 5 matrix (B), but the Matrix A has the following content:
1 2 3
4 5 6
7 8 9
And the resulting bigger matrix B, needs to have the following content:
1 0 2 0 3
0 0 0 0 0
4 0 5 0 6
0 0 0 0 0
7 0 8 0 9
I know this can be done with some "Fors" following a sequence like:
%% We get the dimensions of our matrix.
[xLength, yLength] = size(InMat);
%% We create a matrix of the double size.
NewInMat = zeros(xLength * 2, yLength * 2);
%% We prepare the counters to fill the new matrix.
XLenN = (xLength * 2) -1;
YLenN = (yLength * 2) - 1;
for i = 1 : XLenN
for j = 1 : YLenN
if mod(i, 2) ~= 0
if mod(j, 2) ~= 0
NewInMat(i, j) = InMat(i, j);
else
NewInMat(i,j) = mean([InMat(i, j - 1), InMat(i, j + 2)]);
end
end
end
end
But I would like to know if there is an easier way, or if Matlab has a tool for doing this task. Many thanks in advance!
Upvotes: 1
Views: 32
Reputation: 15837
You can use indexing:
InMat = [...
1 2 3
4 5 6
7 8 9];
s = size(InMat)*2-1;
NewInMat(1:2:s(1), 1:2:s(2)) = InMat;
Here NewInMat
is allocated and filled at the same time.
Upvotes: 5