0

I have a list of lists that I convert into a matrix.

m = [[0, 2, 1, 0, 0], [0, 0, 0, 3, 4], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

n = np.matrix(m)

How do I go about creating a new matrix based on the first three rows and columns?

Specifically, this:

I = [[0, 2, 1], [0, 0, 0], [0, 0, 0]]

I thought the following line would work

I = m[0:3, 0:3]

but I get the error

TypeError: list indices must be integers or slices, not tuple

2 Answers 2

3

you are slicing a list instead of a matrix. list cannot take tuple as an argument. Use n[0:3,0:3] instead of m[0:3, 0:3].

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

2 Comments

It may benefit the asker to have an explanation of why this is correct.
Looks more like a typo. Why make n and not use it? The subject line also mentions the matrix.
3

In addition to user1753919's answer, you can slice the list with following code:

In [10]: [row[0:3] for row in m[0:3]]
Out[10]: [[0, 2, 1], [0, 0, 0], [0, 0, 0]]

But I recommend you to do that with the matrix. Because it's more simple.

Comments

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.