2

I am obtaining several values of two matrices L and R of expected dimensions of 2200x88 for each ith iteration of a for loop using Numpy (Python). some of the matrices have fewer elements for example 1200x55, in those cases I have to add zeros to the matrix to reshape it to be of 2200x88 dimension. I have created the following program to solve that problem:

        for line in infile:
        line = line.strip()
        #more code here, l is matrix 1 of the ith iteration, r is matrix 2 of ith iteration 
ls1, ls2=l.shape
rs1, rs2= r.shape
            if ls1 != 2200 | ls2 != 88:
                l.resize((2200, 88), refcheck=False)
            if rs1 != 2200 | rs2 != 88:
                r.resize((2200, 88), refcheck=False)
            var = np.concatenate((l, r), axis=0).reshape(1,387200)

The problem is that when a matrix R or L is detected not to be of dimensions 2200 by 88 I obtain the following error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Has anyone any recommendations on how to solve this?

Thank you.

user3025898
  • 561
  • 1
  • 6
  • 19

1 Answers1

1

Since l is a matrix, l[0] is a strip of that matrix, an array.

This part:

if l[0] != 2200 | l[1] != 88

is causing your error, since you're trying to "or" two arrays.

So instead of

ls1, ls2=l.shape
rs1, rs2= r.shape
if l[0] != 2200 | l[1] != 88:
    l.resize((2200, 88), refcheck=False)
if r[0] != 2200 | r[1] != 88:
    r.resize((2200, 88), refcheck=False)

Consider:

 if l.shape != (2200, 88):
     l.resize((2200, 88), refcheck=False)
 if r.shape != (2200, 88):
     r.resize((2200, 88), refcheck=False)
jedwards
  • 29,432
  • 3
  • 65
  • 92
  • I have tried and I get: ValueError: cannot resize this array: it does not own its data – user3025898 Mar 05 '15 at 19:33
  • Thats a different issue, but consider looking at answers to [this question](http://stackoverflow.com/questions/23253144/numpy-the-array-doesnt-have-its-own-data) – jedwards Mar 05 '15 at 19:34
  • I think the answer may be to do the following c = b.copy() but do I have to create a separate concatenate function to do that? – user3025898 Mar 05 '15 at 19:39
  • It's hard to say without knowing more about your code. If you copy both matrices `ll,rr = l.copy(),r.copy()` then your concatenate line should look like `var = np.concatenate((ll, rr), axis=0).reshape(1,387200)` – jedwards Mar 05 '15 at 19:48