0

I have an assignment from my university. They asked me to analyse a slicing operation and explain it.

A = array([[1,2,3],[4,5,6],[7,8,9]])
A[0,arrange(3)<>0]
A[1,arrange(3)<>1]
A[2,arrange(3)<>2]

The operation to analyse is the following: A[k, arange(n)<>k], where A is a n x n matrix.

The way I understand it, the first k determines the row of the matrix. Now I know that arange(n) creates an array containing n numbers. What I don't understand is the command arange(n)<>k. If I try to reproduce the code on my computer, the program just tells me "invalid syntax" and points at <>.

Can anyone explain to me whats happening?
Thank you!

2
  • just a small observation, it's better to post the code rather than the picture since the picture might expire and then the people who will come to your question for help won't know what's going on. Also we can copy paste the code in our editors and work on it rather than type it. Just for future reference. Commented Aug 5, 2016 at 22:42
  • Thank you. I'll consider it in the future Commented Aug 5, 2016 at 22:46

2 Answers 2

3

<> is a deprecated synonym for !=, removed in Python 3. Your course should not be using it, but they're doing it anyway, and there isn't much you or we can do about it.

You can either use a Python 2 interpreter for your course, or replace <> with !=. If you replace the operator, you'll need to watch out for other Python 2/3 incompatibilites in the future and handle them manually too; if you switch interpreters, you'll have to manage multiple interpreters. The choice is up to you.

Sign up to request clarification or add additional context in comments.

6 Comments

ok. Thank you. So the operation A[k, arange(n) != k] takes first a row of the matrix, than creates an array [0,1,2] which represent the indexes of the elements from the row and eliminates the one that equals the one of the row. Is that correct?
@BobaJ: It's not quite correct. Your description is unclear enough that I can't tell for sure how wrong your mental model is - it could only be slightly off, or it could be more seriously wrong. Try A[arange(3)!=0, arange(3)!=1] and see if that behaves the way you expect.
@BobaJ Thats correct, except replace the "first row", with "kth row", then the arange is on the kth column
@user2357112: Ok. It didn't behave the way I expected. I undestand the pattern in the example they gave us. But i don't undestand why I got the result of the example you gave me. Could you explain it?
@BobaJ: The full explanation is a lot more complicated, possibly too complicated to actually provide the whole thing as an answer to your university assignment. The documentation explains it all, but it's pretty dense. The short-ish version is that arange(n)!=k is an array of booleans, and for indexing, boolean arrays get converted to an array of integers representing which entries of the boolean array are True. [cont]
|
0

I'm not that much into Python and I don't know if your code should run correctly, but the pattern I see in provided snippet is:

get all elements from k'th row excluding k'th element from that row.

Note that indexes are enumarated from 0.

2 Comments

I believe the question was why the syntax is invalid, not explain the pattern.
Oh, the question was "what is happening", I assumed author wasn't sure what does it do at all. But I agree, I could misunderstand.

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.