Matrize umsortieren

mit matplotlib, NumPy, pandas, SciPy, SymPy und weiteren mathematischen Programmbibliotheken.
Antworten
Kniffte
User
Beiträge: 64
Registriert: Dienstag 27. September 2016, 11:05

Hallo Zusammen,

Folgendes Situation:

Jeder Numpy Array einer Liste enthält x,y,z Koordinaten mehrerer Punkte.

Code: Alles auswählen

liste_numpy_arrays = 
[array([[ 1, 2, 3], [4, 5, 6], [7, 8, 9]]),
 array([[ 1, 2, 3], [4, 5, 6], [7, 8, 9]]),
 array([[ 1, 2, 3], [4, 5, 6], [7, 8, 9]])]
Ich würde diese gern umsortieren, so dass die ersten Punkte jedes einzelnen Numpy Arrays einen neuen Array ergeben und die zweiten ... und die jeweisl dritten usw.:

Code: Alles auswählen

liste_numpy_arrays = 
[array([[ 1, 2, 3], [ 1, 2, 3], [ 1, 2, 3]]),
 array([[4, 5, 6], [4, 5, 6], [4, 5, 6]]),
 array([[7, 8, 9], [7, 8, 9], [7, 8, 9]])]
gibt es das eine elegante Lösung unter verwendung der Methode zur Manipulation von Numpy arrays oder muss ich mir da mit Indexing und slicing was zusammenbasteln?

Gruß Seb
Benutzeravatar
ThomasL
User
Beiträge: 1366
Registriert: Montag 14. Mai 2018, 14:44
Wohnort: Kreis Unna NRW

Code: Alles auswählen

quelle = np.array([np.array([[ 1, 2, 3], [4, 5, 6], [7, 8, 9]]),  np.array([[ 1, 2, 3], [4, 5, 6], [7, 8, 9]]),  np.array([[ 1, 2, 3], [4, 5, 6], [7, 8, 9]])])
ziel = np.array([quelle[:,0,:], quelle[:,1,:], quelle[:,2,:]])
ziel

Code: Alles auswählen

array([[[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]],

       [[4, 5, 6],
        [4, 5, 6],
        [4, 5, 6]],

       [[7, 8, 9],
        [7, 8, 9],
        [7, 8, 9]]])
Ich bin Pazifist und greife niemanden an, auch nicht mit Worten.
Für alle meine Code Beispiele gilt: "There is always a better way."
https://projecteuler.net/profile/Brotherluii.png
narpfel
User
Beiträge: 643
Registriert: Freitag 20. Oktober 2017, 16:10

@Kniffte: Warum hast du eine Liste von Arrays? Wenn du ein dreidimensionales Array hättest, könntest du das Array einfach mit der passenden Vertauschung transponieren:

Code: Alles auswählen

In [25]: list_of_points
Out[25]:
[array([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]]), array([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]]), array([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])]

In [26]: ps = np.array(list_of_points)

In [27]: ps
Out[27]:
array([[[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]],

       [[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]],

       [[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]]])

In [28]: ps.transpose(1, 0, 2)
Out[28]:
array([[[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]],

       [[4, 5, 6],
        [4, 5, 6],
        [4, 5, 6]],

       [[7, 8, 9],
        [7, 8, 9],
        [7, 8, 9]]])

In [32]: ps.transpose?
Docstring:
a.transpose(*axes)

Returns a view of the array with axes transposed.

For a 1-D array, this has no effect. (To change between column and
row vectors, first cast the 1-D array into a matrix object.)
For a 2-D array, this is the usual matrix transpose.
For an n-D array, if axes are given, their order indicates how the
axes are permuted (see Examples). If axes are not provided and
``a.shape = (i[0], i[1], ... i[n-2], i[n-1])``, then
``a.transpose().shape = (i[n-1], i[n-2], ... i[1], i[0])``.

Parameters
----------
axes : None, tuple of ints, or `n` ints

 * None or no argument: reverses the order of the axes.

 * tuple of ints: `i` in the `j`-th place in the tuple means `a`'s
   `i`-th axis becomes `a.transpose()`'s `j`-th axis.

 * `n` ints: same as an n-tuple of the same ints (this form is
   intended simply as a "convenience" alternative to the tuple form)

Returns
-------
out : ndarray
    View of `a`, with axes suitably permuted.

See Also
--------
ndarray.T : Array property returning the array transposed.

Examples
--------
>>> a = np.array([[1, 2], [3, 4]])
>>> a
array([[1, 2],
       [3, 4]])
>>> a.transpose()
array([[1, 3],
       [2, 4]])
>>> a.transpose((1, 0))
array([[1, 3],
       [2, 4]])
>>> a.transpose(1, 0)
array([[1, 3],
       [2, 4]])
Type:      builtin_function_or_method

In [33]:
Antworten