Week 2 Programming Assignment - Column Index Question

Why does this have the column index as -1? I would have thought the index for the last column was 4. See array below.

In Python, an index of -1 always refers to the last row or column.

1 Like

And the cool thing about doing it that way is that it always gives you the last column (or row), regardless of the dimensions of the input matrix. So it makes it easy to write general code.

Index -2 gives you the second to last, -3 the third to last and so forth …

Python does have some handy features - even if they’re not always obvious.

1 Like

EDIT: My post was fueled by a gross misunderstanding of everything. Reading this will only confuse you more.

I have another question about this function, regarding how it gets to index 3.

If the function is starting at column = -1 (the last column) and row = 2, then it’s working on row [0 0 0 0 0].

I’m assuming that, starting at column = -1, the function is working itself backwards (right to left)?

I would expect the index returned to be 4, since counting the indexes in the row, the 7 is 0, 1, 2, 3, 4 numbers into the row. It’s likely I’m not understanding the concept of index.

Never mind, I think I understand. Starting at column = -1, row = 2, the function is is counting downward vertically, and the row (or index?) with the first non-zero value is row = 3 (the answer), which is also the first row with a non-zero value given the input parameters of the function.

Why is the row the index though? Maybe clarification on terminology would be helpful.

I’m not sure what you are asking. Are you asking about the difference between a row and a column?

The row index is the first index and the column index is the second index.

This may or may not help, I’m not sure I understand your question.

Rows and columns can be indexed two different ways:

  • 0 to n-1, shown in fuscia for the rows only.
  • or -n to -1, shown in blue for the columns.
    image

Unfortunately yeah, I was getting confused about the difference between a row and a column :stuck_out_tongue:

The following function in the assignment does what I was assuming this function did.

But the only sort of indexing trick you’re ever going to see in practice is the “-1” variety (to identify the last row or column).