OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to get python 2d numpy arrays of shape 2x2 from 4 1d arrays

  • Thread starter Thread starter squashed.bugaboo
  • Start date Start date
S

squashed.bugaboo

Guest
Suppose I have 4 component arrays of size 6 (e.g., denoting 6 spatial locations in a grid):

Sxx = array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0])

Sxy = array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6])

Then assume Syx = Sxy, and

Syy = array([1.1, 2.1, 3.1, 4.1, 5.1, 6.1])

How do I retrieve the above arrays as 6 2×2 arrays, e.g., as below, in a "pythonic" way?

S = array([ [[1.0, 0.1], [0.1, 1.1]], [[2.0, 0.2], [0.2, 2.1]], [[3.0, 0.3], [0.3, 3.1]], .... ])
I could even work with 6 1-d 1x4 arrays as:

S = [ [1.0, 0.1, 0.1, 1.1], [2.0, 0.2, 0.2, 2.1], [3.0, 0.3, 0.3, 3.1], .... ]
Any ideas are much appreciated!

Edit: It looks like I stumbled upon the answer using dstack combined with reshape!!
<p>Suppose I have 4 component arrays of size 6 (e.g., denoting 6 spatial locations in a grid):</p>
<p><code>Sxx = array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0])</code></p>
<p><code>Sxy = array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6])</code></p>
<p>Then assume <code>Syx = Sxy</code>, and</p>
<p><code>Syy = array([1.1, 2.1, 3.1, 4.1, 5.1, 6.1])</code></p>
<p>How do I retrieve the above arrays as 6 2×2 arrays, e.g., as below, in a "pythonic" way?</p>
<code>
S = array([
[[1.0, 0.1], [0.1, 1.1]],
[[2.0, 0.2], [0.2, 2.1]],
[[3.0, 0.3], [0.3, 3.1]],
....
])
</code>
<p>I could even work with 6 1-d 1x4 arrays as:</p>
<code>
S = [
[1.0, 0.1, 0.1, 1.1],
[2.0, 0.2, 0.2, 2.1],
[3.0, 0.3, 0.3, 3.1],
....
]
</code>
<p>Any ideas are much appreciated!</p>
<p>Edit: It looks like I stumbled upon the answer using dstack combined with reshape!!</p>
 

Latest posts

Online statistics

Members online
0
Guests online
2
Total visitors
2
Top