Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagepy
mat1 = [[1, 2, 3], [4, 5, 6]]
# To make the matrices easier to read while coding, we can place the different lists vertical to eachother, rather than horizontally.
mat2 = [[1, 2, 3],
	   [4, 5, 6]]


# Printing both of these matrices will result in the same output:
[[1, 2, 3], [4, 5, 6]]


# To alter the output to a more readable format, for-loops are very helpful:
[[1, 2, 3], 
[4, 5, 6]]

Now, let's see how to obtain differnt values from our list:

Code Block
languagepy
A = [[1, 2, 3], 
	[4, 5, 6]]


print("A =", A) 
print("A[1] =", A[1])      		# 2nd row
print("A[1][2] =", A[1][2])   	# 3rd element of 2nd row
print("A[0][-1] =", A[0][-1])   # Last element of 1st Row

column = [];        			# empty list
for row in A:
  column.append(row[2])   

print("3rd column =", column)


# This script will result in the following output:


A = [[1, 2, 3], [4, 5, 6]]
A[1] = [4, 5, 6]
A[1][2] = 6
A[0][-1] = 3
3rd column = [3, 6]




Code Block
languagepy
titleCreating a matrix
a = np.array([[1,2],[3,4]])

...