views:

569

answers:

6

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?

A: 

There is a Reshape() function that allows you to do this...

For example:

reshape(array, [64, 16])

And you can find a great video tutorial here

Cheers

Anthony M. Powers
reshape will not work for this--it requires that the input and output matrices have the same number of elements.
las3rjock
thanks for a quick one but i think for reshape the number of elements must not change. Here i have a 2X2 matrix ie 4 elements and i want to make a 4X4 matrix so 16 elements.No of elements are different. Wat to do?
anubhav
Are you desiring the 'Reshape' process to add relevant element data in the newly-created space?
Anthony M. Powers
+2  A: 

This works:

A = [2 6; 8 4];
[X,Y] = meshgrid(1:2);
[XI,YI] = meshgrid(0.5:0.5:2);
B = interp2(X,Y,A,XI,YI,'nearest');

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:

A = [2 6; 8 4];
B = A([1 1 2 2], [1 1 2 2]);
las3rjock
will this work for any generic 2X2 matrix or is only specific to this 2X2 matrix.
anubhav
This will work for any generic 2x2 matrix.
las3rjock
great. it works. thanks
anubhav
+2  A: 
A = [2 6; 8 4];
% arbitrary 2x2 input matrix

B = repmat(A,2,2);
% replicates rows & columns but not in the way you want

B = B([1 3 2 4], :);
% swaps rows 2 and 3

B = B(:, [1 3 2 4]);
% swaps columns 2 and 3, and you're done!
Jason S
Wow. That was a simple and good answer. Thanks
anubhav
Nice! I thought there should be some way to do this with repmat and column and row swapping, but my brain is still half-asleep.
las3rjock
Solution by gnovice using Kroneker KRON function is better - it is more straightforward and clear - "intentional programming"
Mikhail
I like Martijn's answer better but that's just me.
Jason S
With respect to all the indexing-based solutions, I actually prefer Edric's (which is a generalized form of las3rjock's edit).
gnovice
The mathematical operation in question is called "Kronecker product"http://en.wikipedia.org/wiki/Kronecker_product. The solution by gnovice does exactly this, directly. Other solutions, whatsoever clever, are detours.
Mikhail
Kronecker product is more general and allows multiplication. OP wanted to replicate matrix elements to form a block matrix. KRON function documentation, IMHO, is confusing and its use is less clear for this purpose. But we can agree to disagree.
Jason S
+9  A: 

Can be done even easier than Jason's solution:

B = A([1 1 2 2], :);
B = B(:, [1 1 2 2]);
Martijn
:p :-) .........
Jason S
this answer is better!
Jason S
One-liner: A = [2 6; 8 4]; B = A([1 1 2 2], [1 1 2 2]);
las3rjock
Shouldn't the first line be `B = A([1 1 2 2],:);`?
gnovice
+4  A: 

Here's one more solution:

A = [2 6; 8 4];
B = A( ceil( 0.5:0.5:end ), ceil( 0.5:0.5:end ) );

which uses indexing to do everything and doesn't rely on the size or shape of A.

Edric
that is bizarre! somehow the "end" keyword knows about matrix A even though it's an argument to ceil(). I have no idea how that works but it does.
Jason S
I have no idea how it does it either, but it's handy!
Edric
crazy, ugly and the most beautiful solution at the same time
Mikhail
+11  A: 

Here's an even shorter solution which uses the MATLAB functions KRON and ONES:

>> A = [2 6; 8 4];
>> B = kron(A,ones(2))

B =

     2     2     6     6
     2     2     6     6
     8     8     4     4
     8     8     4     4
gnovice
shorter, although for generalization to large matrices, there's unnecessary multiplication that may slow things down.
Jason S
@Jason S: True, but for larger matrices you would also have to do quite a bit more work in generating the indices needed by the other solutions here.
gnovice

related questions