'After importing a matlab data set, how to pick a a specific column to manipulate?

I am working on a project and there is a matlab file with multiple lists of number and I have to pull out two columns to later do multiple regression on. How would I go about doing this.

enter image description here



Solution 1:[1]

You didn't put data as text so it is hard to say how it looks - and we can't use your data in answer.

It seems you have dictionary with numpy arrays - like this:

data = {
  'Data1': array(...),
  'Data2': array(...),
}

To get one numpy array you can use key - ie. data["Data1"] - and then you have can use indexes to get columns - ie. data["Data1"][:,0:2]

If you want to get from all arrays then you can use for-loop to work with every array separatelly - and add or append columns to some list or array

results = []

for key in data.keys(): 
    results.append( data[key][:,0:2] )

or

results = []

for array in data.values(): 
    results.append( array[:,0:2] )

Minimal working example

import numpy as np

np.random.seed(0)  # to get always the same data

data = {
    'Data1': np.random.rand(5, 3),
    'Data2': np.random.rand(5, 3),
}

print('--- data ---')
print(data)

print("--- data['Data2'] ---")
print(data['Data2'])

print("--- data['Data2'][:,0:2] ---")
print(data['Data2'][:,0:2])

print('--- for-loop ---')
results = []
for array in data.values():
    columns = array[:,0:2]
    print(columns)
    results.append(columns)
    
print('--- results ---')
print(results)
    
print('--- array ---')
array = np.array(results).reshape((-1,2))
print(array)

Result:

--- data ---
{'Data1': array([[0.5488135 , 0.71518937, 0.60276338],
       [0.54488318, 0.4236548 , 0.64589411],
       [0.43758721, 0.891773  , 0.96366276],
       [0.38344152, 0.79172504, 0.52889492],
       [0.56804456, 0.92559664, 0.07103606]]), 'Data2': array([[0.0871293 , 0.0202184 , 0.83261985],
       [0.77815675, 0.87001215, 0.97861834],
       [0.79915856, 0.46147936, 0.78052918],
       [0.11827443, 0.63992102, 0.14335329],
       [0.94466892, 0.52184832, 0.41466194]])}
--- data['Data2'] ---
[[0.0871293  0.0202184  0.83261985]
 [0.77815675 0.87001215 0.97861834]
 [0.79915856 0.46147936 0.78052918]
 [0.11827443 0.63992102 0.14335329]
 [0.94466892 0.52184832 0.41466194]]
--- data['Data2'][:,0:2] ---
[[0.0871293  0.0202184 ]
 [0.77815675 0.87001215]
 [0.79915856 0.46147936]
 [0.11827443 0.63992102]
 [0.94466892 0.52184832]]
--- for-loop ---
[[0.5488135  0.71518937]
 [0.54488318 0.4236548 ]
 [0.43758721 0.891773  ]
 [0.38344152 0.79172504]
 [0.56804456 0.92559664]]
[[0.0871293  0.0202184 ]
 [0.77815675 0.87001215]
 [0.79915856 0.46147936]
 [0.11827443 0.63992102]
 [0.94466892 0.52184832]]
--- results ---
[array([[0.5488135 , 0.71518937],
       [0.54488318, 0.4236548 ],
       [0.43758721, 0.891773  ],
       [0.38344152, 0.79172504],
       [0.56804456, 0.92559664]]), array([[0.0871293 , 0.0202184 ],
       [0.77815675, 0.87001215],
       [0.79915856, 0.46147936],
       [0.11827443, 0.63992102],
       [0.94466892, 0.52184832]])]
--- array ---
[[0.5488135  0.71518937]
 [0.54488318 0.4236548 ]
 [0.43758721 0.891773  ]
 [0.38344152 0.79172504]
 [0.56804456 0.92559664]
 [0.0871293  0.0202184 ]
 [0.77815675 0.87001215]
 [0.79915856 0.46147936]
 [0.11827443 0.63992102]
 [0.94466892 0.52184832]]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1