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!

limbo
  • 684
  • 9
  • 18
BobaJ
  • 1
  • 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. – limbo Aug 05 '16 at 22:42
  • Thank you. I'll consider it in the future – BobaJ Aug 05 '16 at 22:46

2 Answers2

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.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • 1
    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 Aug 05 '16 at 22:22
  • @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. – user2357112 Aug 05 '16 at 22:28
  • @BobaJ Thats correct, except replace the "first row", with "kth row", then the arange is on the kth column – OneCricketeer Aug 05 '16 at 22:31
  • @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 Aug 05 '16 at 22:34
  • @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](http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#advanced-indexing) 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] – user2357112 Aug 05 '16 at 22:59
  • After boolean arrays in the indexing expression are converted, all objects between the brackets are [broadcast](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) against each other, so `A[0, array([1, 2])]` becomes `A[array([0, 0]), array([1, 2])]`, and `A[array([0, 2]), array([1, 2])]` is unaffected. NumPy then builds an array such that for an expression `A[X, Y]`, we have `A[X, Y][i] == A[X[i], Y[i]]`, and that's the final output. – user2357112 Aug 05 '16 at 23:04
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.

Jacek
  • 829
  • 12
  • 31
  • I believe the question was why the syntax is invalid, not explain the pattern. – OneCricketeer Aug 05 '16 at 22:24
  • 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. – Jacek Aug 05 '16 at 22:28