I need some help in converting a 2X2 matrix to a 4X4 matrix in the following manner:
A = [2 6;
8 4]
should become:
B = [2 2 6 6;
2 2 6 6;
8 8 4 4;
8 8 4 4]
How would I do this?
I need some help in converting a 2X2 matrix to a 4X4 matrix in the following manner:
A = [2 6;
8 4]
should become:
B = [2 2 6 6;
2 2 6 6;
8 8 4 4;
8 8 4 4]
How would I do this?
There is a Reshape() function that allows you to do this...
For example:
And you can find a great video tutorial here
Cheers
This works:
This is just two-dimensional nearest-neighbor interpolation of A(x,y) from x,y ∈ {1,2} to x,y ∈ {0.5, 1, 1.5, 2}.
Edit: Springboarding off of Jason S and Martijn's solutions, I think this is probably the shortest and clearest solution:
In newer versions of MATLAB (R2015a and later) the easiest way to do this is using the
repelem
function:For older versions, a short alternative to the other (largely) indexing-based solutions is to use the functions
kron
andones
:Here's one more solution:
which uses indexing to do everything and doesn't rely on the size or shape of A.
Here's a method based on simple indexing that works for an arbitrary matrix. We want each element to be expanded to an MxN submatrix:
Example:
To see how the method works, let's take a closer look at the indexing. We start with a simple row vector of consecutive numbers
Next, we extend it to a matrix, by repeating it M times in the first dimension
If we use a matrix to index an array, then the matrix elements are used consecutively in the standard Matlab order:
Finally, when indexing an array, the 'end' keyword evaluates to the size of the array in the corresponding dimension. As a result, in the example the following are equivalent: