Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with matricies
#1
Before anyone crucify me for asking a stupid question, I have to point out that I started learning Python 2 weeks ago (and I don't have much time per day, so progress is a bit slow...)
Having some experience mainly with C++ (some other languages as well) it was mainly learning language syntax and it was, and still is, easy. But, I believe I am missing something when it comes to matrices, so I figured I should ask here.
Now, I know I can use NymPy, but I want to understand how Python basics operate.
Let’s say I want to declare 3x3 matrix. To my surprise it was a bit more complicated than simply writing MatrixA[a][b][c]…
So, I learned that in Python you have to declare array that will be a member of another array and so on..
But, here is my question:
Let say I want to change Matrix[i][j][k]=AnyValue
This doesn’t work. Simple (and probably stupid solution) would be to change initial array first – make changes on the k-place. Then put that array in the second one (2x2 matrix) on the “j” place and so on…
But, I am sure I’m missing something here. It can’t be that complicated.
So, how do I address particular member of the array and how do I change it?

Sorry, for a long post…
Reply
#2
first of all as per the docs, it's not recommended to use matrix, which may be removed. use array (ndarray)

So check Array creation and Array creation routines


some examples
import numpy as np

shape = (3, 3) # define a shape as tuple
# Random values
m = np.empty(shape)
print(m, type(m))
# 1s
m = np.ones(shape)
print(m, type(m))
# 0s
m = np.zeros((3, 3)) # you can pass shape tuple directly
print(m, type(m))
# convert it to matrix
m = np.matrix(m)
print(m, type(m))

# identity array
m = np.identity(3)
print(m, type(m))

# fill it with nans
m.fill(np.nan)
print(m, type(m))

# create matrix from data
m = np.matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(m, type(m))
Output:
[[6.93090232e-310 1.10219722e-316 6.81092251e+199] [1.02420083e-259 8.50959917e-096 1.63041663e-322] [1.09670637e-316 1.09543128e-316 3.95252517e-322]] <class 'numpy.ndarray'> [[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]] <class 'numpy.ndarray'> [[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]] <class 'numpy.ndarray'> [[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]] <class 'numpy.matrix'> [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]] <class 'numpy.ndarray'> [[nan nan nan] [nan nan nan] [nan nan nan]] <class 'numpy.ndarray'> [[1 2 3] [4 5 6] [7 8 9]] <class 'numpy.matrix'>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Also, you may find this comparison between array and matrix interesting
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Thanks for quick answers.
So, numpy it is...:)
Reply
#5
(May-04-2020, 04:41 PM)ptnb Wrote: So, numpy it is...:)
well, it was you who said that is using numpy :-)

(May-04-2020, 01:56 PM)ptnb Wrote: Now, I know I can use NymPy
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020