Make matrix "n" larger than matlab

I have such a matrix

1 2 3 4 
2 3 4 5
3 4 5 6 

is there a function that does rows n times and columns m times in matlab, I mean, for example, for n = 2 and m = 3, the result:

1 1 1 2 2 2 3 3 3 4 4 4 
1 1 1 2 2 2 3 3 3 4 4 4 
2 2 2 3 3 3 4 4 4 5 5 5 
2 2 2 3 3 3 4 4 4 5 5 5 
3 3 3 4 4 4 5 5 5 6 6 6 
3 3 3 4 4 4 5 5 5 6 6 6 

thank

+5
source share
2 answers

You can use the kronecker product:

 A=[1 2 3 4;5 6 7 8;9 10 11 12];
 kron(A,ones(2,3))
ans =
     1     1     1     2     2     2     3     3     3     4     4     4
     1     1     1     2     2     2     3     3     3     4     4     4
     5     5     5     6     6     6     7     7     7     8     8     8
     5     5     5     6     6     6     7     7     7     8     8     8
     9     9     9    10    10    10    11    11    11    12    12    12
     9     9     9    10    10    10    11    11    11    12    12    12

For more information, you can look at wikipedia:

http://en.wikipedia.org/wiki/Kronecker_product

+9
source

Here is my solution:

%------------------data-----------------
>> mat = [1 2 3 4;2 3 4 5;3 4 5 6]
mat =
     1     2     3     4
     2     3     4     5
     3     4     5     6
>> [m,n] = deal(3,2)
m =
     3
n =
     2
%----------------solution----------------
>> col = meshgrid(1:size(mat,2),1:m);
>> row = meshgrid(1:size(mat,1),1:n);
>> mat(row,col)
ans =
     1     1     1     2     2     2     3     3     3     4     4     4
     1     1     1     2     2     2     3     3     3     4     4     4
     2     2     2     3     3     3     4     4     4     5     5     5
     2     2     2     3     3     3     4     4     4     5     5     5
     3     3     3     4     4     4     5     5     5     6     6     6
     3     3     3     4     4     4     5     5     5     6     6     6
0
source

All Articles