1

I'm trying to append a 4x1 row of data onto a matrix in python. The matrix is initialized as empty, and then grows by one row during each iteration of a loop until the process ends. I won't know how many times the matrix will be appended, so initializing the array to a predetermined final size is not an option unfortunately. The issue that I'm finding with np.r_ is that the matrix and list being appended need to be the same size, which is rarely the case. Below is some pseudocode of what I've been working with.

    import numpy as np

    dataMatrix = np.empty([4,1])

    def collectData():
            receive data from hardware in the form of a 4x1 list

    while receivingData:
            newData = collectData()
            dataMatrix = np.r_(dataMatrix, newData)

Does anyone have an idea of how to go about finding a solution to this issue?

youngpadwan
  • 31
  • 1
  • 6
  • related: http://stackoverflow.com/questions/5064822/how-to-add-items-into-a-numpy-array – NightShadeQueen Jul 16 '15 at 20:15
  • A common `numpy` approach is to append these values to a list, and turn that into an array when you are all done. List is better for iterative appending than arrays. – hpaulj Jul 16 '15 at 22:36
  • `np.r_` is a fancy front end to 1d concatenate. It's most useful for stringing together a mixed bag of ranges and linspace, expressed as slices. – hpaulj Jul 17 '15 at 00:15

1 Answers1

0

As @hpaulj suggested you should use a list of lists and then convert to a NumPy matrix at the end. This will be at least 2x faster than building up the matrix using np.r_ or other NumPy methods

import numpy as np

dataMatrix = []

def collectData():
        return 4x1 list

while receivingData:
        dataMatrix.append(collectData())

dataMatrix = np.array(dataMatrix)

As a sidenote, with np.r_ the only requirement is that the first dimension of the matrix be equal to the first (and only, in your case) dimension of the array. Perhaps you used np.r_ when you should have used np.c_

Community
  • 1
  • 1
rohanp
  • 610
  • 1
  • 8
  • 20