Is there an way to create matrix[a]**[c][d] in python? It seems that you can write this way in Perl but not Python? Thanks!**

pythonstarter
0
Newbie Poster

## Recommended Answers

Jump to PostA 2 dimensional matrix can easily be implemented as a list of lists

`M = [ [0, 1, 2], # first row [3, 4, 5], # second row [6, 7, 8], # third row ]`

For heavy computations with matrices, there is a specialized module

`numpy`

…

Jump to PostIt's not at all difficult, here is a code which creates a 4-dimensional tensor of size 2 x 4 x 3 x 2

`# python 2 or 3 def zero_matrix(shape): if len(shape) == 1: return [0] * shape[0] else: return [zero_matrix(shape[1:]) for i in range(shape[0])] if __name__ …`

Jump to PostIt is a recursive function. The 'shape' argument is a non empty

tuplelike (2, 4, 3, 2).

Suppose by induction on the length of 'shape' that zero_matrix returns a tensor with the given shape.

Then in line 6,`shape[1:]`

is (4, 3, 2), and`shape[0]`

is 2, so zero_matrix((4, …

## All 9 Replies

Gribouillis
1,391
Programming Explorer
Team Colleague

pythonstarter
0
Newbie Poster

Gribouillis
1,391
Programming Explorer
Team Colleague

pythonstarter
0
Newbie Poster

Gribouillis
1,391
Programming Explorer
Team Colleague

pythonstarter
0
Newbie Poster

TrustyTony
888
pyMod
Team Colleague
Featured Poster

TrustyTony
888
pyMod
Team Colleague
Featured Poster

TrustyTony
888
pyMod
Team Colleague
Featured Poster

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.