Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am new to python so I am sorry if my formatting, etc is bad or if this question is trivial but I am trying to figure out why I am getting an index error in the third (and I assume second) to last lines of my code. I include the function just for context so you can see the QR function that the array I am calling is derived from. The command giving the index error works in the second line of the code but not the second to last. Can anyone explain why this is? I am thinking it has something to do with the fact that a_new is an array?

Also, from what I understand a[:,0] takes the first column from a matrix or array. If this understanding is not correct I would love to learn the right way to do this too.

Thanks!

a=np.random.random((3,3)); a
a[:,2]


###Gram-Schmidt operation for finding Q orthonormal matrix
###We begin with the reduced 3X3 case
def QR(a):  
     A0=a[:,0]; A0
     A1=a[:,1]; A1
     A2=a[:,2]; A2   

     Z0=A0
     Z1=A1-np.vdot(A1,A0)/np.vdot(A0,A0)*A0
     Z2=A2-np.vdot(A2,A0)/np.vdot(A0,A0)*A0-np.vdot(A2,Z1)/np.vdot(Z1,Z1)*Z1

     Z0norm=np.linalg.norm(Z0); Z0norm
     Z1norm=np.linalg.norm(Z1); Z1norm
     Z2norm=np.linalg.norm(Z2); Z2norm

     q0=Z0/Z0norm
     q1=Z1/Z1norm
     q2=Z2/Z2norm

     a_new=np.array([[q0],[q1],[q2]])

     return a_new

a_new=QR(a); a_new
a_new0=a_new[:,0]
a_new1=a_new[:,1]
g1=np.vdot(a_new1,a_new2); g1   

EDIT: Sorry! Here is the error message:

a_new0=a_new[:,0] a_new1=a_new[:,1] Traceback (most recent call last): File "", line 1, in IndexError: index 1 is out of bounds for axis 1 with size 1

share|improve this question
1  
Did you even see the error message? Perhaps that would have made the problem obvious? –  devnull Feb 15 at 20:01
    
I did. But didn't understand it. IndexError: index 1 is out of bounds for axis 1 with size 1 How does that makes sense for a 3X3 matrix? That is sort of the crux of my question. Why would I get this for a 3X3? –  sfortney Feb 15 at 20:02
    
Hmmm if only other people could see the error message. Maybe if someone posted it here... –  Carsten Feb 15 at 20:03
1  
Why are you so convinced that a_new is 3x3? Did you try looking at it, or checking a_new.shape? (Spoiler alert: it's not.) –  DSM Feb 15 at 20:06
    
Oh, Thanks. I think that answers my question. I feel dumb now. Thank you :) –  sfortney Feb 15 at 20:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.