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.

4
  • I suspect that l[0] != 2200 | l[1] != 88 should be ls1 != 2200 | ls2 != 88 and the same thing goes for the r[0]... line 2 lines below. Commented Mar 5, 2015 at 19:18
  • yes, my bad! I copied the wrong code. Thank you for spotting it. Commented Mar 5, 2015 at 19:30
  • You need to clean up the indentation, e.g. the for line. Also are you missing some if statements? Commented Mar 6, 2015 at 0:00
  • what code define l.shape and r.shape? Commented May 31, 2021 at 13:43

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

4 Comments

I have tried and I get: ValueError: cannot resize this array: it does not own its data
Thats a different issue, but consider looking at answers to this question
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?
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.