0

I would like to expand the size of 3D matrices by N times.

Take 2D example with N=3,

[1 2; 
 3 4]  

should be transformed to

[1 1 1 2 2 2 ;
 1 1 1 2 2 2; 
 1 1 1 2 2 2; 
 3 3 3 4 4 4 ;
 3 3 3 4 4 4 ;
 3 3 3 4 4 4 ]

Thanks very much.

Although the example is 2D, what I really need is a solution for 3D matrices. The function kron() does not work for 3D.

nos
  • 19,875
  • 27
  • 98
  • 134

1 Answers1

4

2D case

You can use kron:

kron(A, ones(N,N))

Or use indexing:

A(ceil(1/N:1/N:end), ceil(1/N:1/N:end))

3D case

kron doesn't work for 3D, but indexing does:

A = cat(3, [1 2; 3 4], [10 20; 30 40]);
A(ceil(1/N:1/N:end), ceil(1/N:1/N:end), ceil(1/N:1/N:end))

Of course, you could use different values of "N" along each dimension, say N1, N2, N3:

A(ceil(1/N1:1/N1:end), ceil(1/N2:1/N2:end), ceil(1/N3:1/N3:end))
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • what if my matrix is 3D? do I do it slice by slice? – nos Aug 22 '14 at 15:06
  • Please see edited answer. I'm assuming you expand by `N` also in the third dim – Luis Mendo Aug 22 '14 at 15:08
  • thank you very much. May I also ask another question: suppose I do some operation on the expanded matrix and later I want to map it back to the original dimension by summing over entries in the same old blocks. Is there an efficient way to do it? – nos Aug 22 '14 at 15:17
  • @nos The simplest way to do that is using [`blockproc`](http://www.mathworks.es/es/help/images/ref/blockproc.html) from the Image Processing Toolbox. Another approach is to keep track of the mapping with an auxiliary "labelling" matrix containing integers, and then use [`accumarray`](http://www.mathworks.es/es/help/matlab/ref/accumarray.html) with those labels as first argument. That may be faster, but requires extra memory for the labelling variable. Give it a try and post a new question if needed – Luis Mendo Aug 22 '14 at 15:21
  • `kron` is not time efficient. Use `repmat` and then `reshape`. For more information see [this answer](http://stackoverflow.com/questions/25369159/expand-an-array-by-filling-in-with-current-values-in-matlab/25372072#25372072). – Nematollah Zarmehi Aug 22 '14 at 17:34
  • 1
    @NematollahZarmehi - Certain sequences of operations in terms of timing will differ depending on **what** you are doing. The only way you can really gauge is to time this situation, like you did for your other post. In one post that I benchmarked, a `for` loop was the most time efficient solution due to the kicking in of the JIT compiler, even though `for` loops are naturally frowned upon in MATLAB. See this post for more details: http://stackoverflow.com/questions/23879888/matlab-subtracting-matrix-subsets-by-specific-rows/23880086#23880086 – rayryeng Aug 22 '14 at 20:02
  • @NematollahZarmehi - I also cannot see this being efficiently done using `repmat` and `reshape` without doing at least 5 other statements. Can you show us how you would approach this using `repmat` and `reshape`? I'm just curious is all! :) – rayryeng Aug 22 '14 at 20:04