63130

Interlace various small 2D matrices into a bigger one

Question:

I have some small matrices that I would like to interlace into a bigger one. Lets say for example that I have 4 matrices of 4x4 (although the solution I am looking for should work with non-square 2D matrices too) and I want to combine their results to build a 8x8 matrix like this: <a href="https://i.stack.imgur.com/MLZGy.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/MLZGy.png" data-original="https://i.stack.imgur.com/MLZGy.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

The order in which I get the small matrices tells me the positions of their elements in the bigger matrix.

I guess I could do it iterating value by value and computing the new indices over and over but I am sure that there has to be a much more efficient way to do it.

Answer1:

<strong>Approach #1</strong>

For performance efficiency, I would suggest initializing the output array and then assigning into it with slicing, like so -

A = [a0,a1,a2,a3] # Input list of arrays m,n = A[0].shape out = np.empty((2*m,2*n),dtype=A[0].dtype) out[::2,::2] = A[0] out[::2,1::2] = A[1] out[1::2,::2] = A[2] out[1::2,1::2] = A[3]

Sample run -

1) Input :

In [67]: a0 Out[67]: array([[92, 65, 41, 14], [31, 95, 79, 77], [98, 89, 26, 68], [63, 91, 23, 74]]) In [68]: a1 Out[68]: array([[67, 13, 43, 25], [18, 15, 38, 34], [13, 59, 46, 89], [11, 68, 81, 11]]) In [69]: a2 Out[69]: array([[12, 53, 81, 91], [88, 83, 77, 98], [63, 53, 56, 76], [58, 64, 57, 68]]) In [70]: a3 Out[70]: array([[21, 82, 88, 49], [54, 53, 62, 80], [89, 96, 72, 62], [81, 93, 41, 84]])

2) Output :

In [72]: out Out[72]: array([[92, 67, 65, 13, 41, 43, 14, 25], [12, 21, 53, 82, 81, 88, 91, 49], [31, 18, 95, 15, 79, 38, 77, 34], [88, 54, 83, 53, 77, 62, 98, 80], [98, 13, 89, 59, 26, 46, 68, 89], [63, 89, 53, 96, 56, 72, 76, 62], [63, 11, 91, 68, 23, 81, 74, 11], [58, 81, 64, 93, 57, 41, 68, 84]])

<strong>Approach #2</strong>

We could also use <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.concatenate.html" rel="nofollow">np.concatenate</a> to stack those into one array and then transpose and reshape finally into the intended (2*m,2*n) shaped array, like so -

np.concatenate((A)).reshape(2,2,m,n).transpose(2,0,3,1).reshape(2*m,2*n)

Recommend

  • Simple Membership gives me Unable to find the requested .Net Framework Data Provider
  • Json.net Add property to every class containing of a certain type
  • Tomcat 7 refuses to start on Eclipse when adding new servlet in web.xml DD
  • Why are JQuery autocomplete results not showing in browser?
  • how to make my program to wait for a particular thread and also not affecting my GUI's interact
  • Pandas data types change when iterating over the major axis
  • how C++ Implicitly convert c style string to a string object?
  • Kahan summation algorithm has big computing error when it is compiled by GCC
  • How does sizeof work for int types?
  • Convert “String” of Binary to NSString of text
  • finding maximum depth of chapter
  • Render html in springfox-swagger-ui
  • Making a switch statement in C with an array?
  • How to move axis labels automatically in rgl R
  • Sql indexes vs full table scan
  • Positioning children objects in scene (car wheels hierarchy)
  • Should I optimize around reads or CPU time in Google App Engine
  • C++ String tokenisation from 3D .obj files
  • CakePHP 3: change order in dateWidget
  • Equivalent of branch..merge for git-push
  • C++ Armadillo Access Triangular Matrix Elements
  • Python find continuous interesctions of intervals
  • possible limitation of implode function in PHP
  • Why I can't use uniform1f instead of uniform4f for setting a vec4 uniform?
  • Iterate twice through a DataReader
  • PHP UTF-8 to GB2312
  • What's the name of this finding square root algorithm?
  • netsh acl setting (need alternative method - registry settings?)
  • how to set variables in a php include file?
  • Redshift Querying: error xx000 disk full redshift
  • Rails Find when some params will be blank
  • Recording logins for password protected directories
  • Deselecting radio buttons while keeping the View Model in synch
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • When should I choose bucket sort over other sorting algorithms?
  • How can I estimate amount of memory left with calling System.gc()?
  • Why winpcap requires both .lib and .dll to run?
  • Unanticipated behavior
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`