0

EDIT with more details about the data structure. (Please notice that even if it seems to be a duplicate question, it's a specific case of array to matrix transformation).

In the end of the code below, after applying many operations to the same set of data, I've built a final 3D array by transforming a list of arrays into a 3D array (that's in the last lines of the code, details specified in comments).

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import glob

big_list = [] #list used to store all data built from multiple .txt files.



for a in range(1,21): #loop to open and take data from multiple .txt files.
    for s in range(1,11): 
        for e in range(1,4): 
            #specify path (with vars from loop) 
            path = '/home/user/plots/Files/a{0}_s{1}_e{2}_skeleton.txt'.format(a, s,e) 
            archive = glob.glob(path)
            #if the .txt file's opened, starts operations with data taken from it.      
            if (archive): 
                movement = np.loadtxt(path)
                num_skeletons = int(len(movement)/20)
                list_for_array = [] #list to take all data from the file.
                for i in range(num_skeletons): 
                    #list_for_array is transformed in a 2D array:
                    list_for_array.append(movement[(i*20):((i+1)*20),:3]) 
                #that same 2D array is made into a 3D array:
                coords_array= np.array(list_for_array) 


                # a 3D matrix is made from the 3D array. 
                dist_matrix = np.zeros((num_skeletons,20,20))           
                for k in range(num_skeletons):
                    for i in range(20):
                        for j in range(20):
                            dist_matrix[k,i,j] = np.linalg.norm(coords_array[k,i,:] - coords_array[k,j,:])
                    #end of the 3D matrix construction. 

                #the 3D matrix is made into a list of 2D array (each array with 1 line and 400 columns).
                for m in range(num_skeletons):

                    array = dist_matrix[m].reshape((1,400))
                    #the m 2D arrays are added to a list: 
                    big_list.append(array)
                #dist_matrix = None 

#now, big_list has all the data from all .txt files (many 2D arrays).
big_array = np.array(big_list) #and finally, big_list is made into a 3D array, or, an array made of many 2D arrays.

I didn't explain what's the deep meaning of the code because it would take too long, and the aim here is to know:

How could I transform that final 3D array (big_array) in a 2D matrix ?

E. AMARAL
  • 949
  • 1
  • 10
  • 20
  • 1
    In your thinking, what is a 'pure matrix'? Some mathematical idea? A MATLAB like object? – hpaulj Sep 03 '18 at 16:48
  • I mean just a simple matrix, not a 2D array. I say "pure matrix"because when I run the code in the command line, there's a clear difference in the structure of a 2D array and a matrix. So I just want to make the transformation. – E. AMARAL Sep 03 '18 at 19:09
  • You may need to show some code and examples where you see this difference. Your Description isn't clear. – hpaulj Sep 03 '18 at 19:36
  • After studying better what I'm having problems with, I've found that the aim is to transform a 3D array in a 2D matrix. Therefore, I've updated the whole question. – E. AMARAL Sep 05 '18 at 15:01
  • Looks like a whole new question. Minor edits are ok, but not ones that change the basic question. Few except me will see the changes. – hpaulj Sep 05 '18 at 15:19
  • 1
    `reshape` can change a 3d to 2d. Also consider using `np.vstack` in that last line. – hpaulj Sep 05 '18 at 15:22
  • It just worked, thanks a lot! – E. AMARAL Sep 05 '18 at 16:16

0 Answers0