Learn Python With Dhawal - 13 - Basic Operations on Matrices or 2-D Arrays


Before we head further into deeper sorting techniques, I want to let you know about the options by which you can use Multidimensional Arrays in Python. Although there are many sophisticated and easier ways to deal and operate on matrices or 2-D arrays using Numpy and other similar libraries, but here I want you to learn about how to create and use a 2-D array and perform algebraic operations on them.

How to create a 2-D Array or a Matrix in Python

Technically speaking there's no array in python but these are lists. This way we are creating a list within a list. Basically creating a nested list to use like a matrix.

Code 1 :

Input:

  1. a=[[1,2,3],[4,5,6],[7,8,9]]
  2. print(a)


Output:

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]


I feel this particular code will be self explanatory as the 'print' statements explain the use of the commands.

Code 2 :

Input:

  1. a=[[1,2,3],[4,5,6],[7,8,9]]
  2. print("This is our Initial Matrix ",a)
  3. a.append([1,2,3])
  4. print ("This is matrix after adding one more row at the end ",a)
  5. a.insert(1,[234])
  6. print ("This is the matrix after adding an element at a particular index position ",a)
  7. a[0][2]=345
  8. print ("This is the matrix after editing the value of one element at a particular index ",a)


Output:


  1. This is our Initial Matrix 
  2.  [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  3. This is matrix after adding one more row at the end 
  4.  [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 2, 3]]
  5. This is the matrix after adding an element at a particular index position 
  6.  [[1, 2, 3], [234], [4, 5, 6], [7, 8, 9], [1, 2, 3]]
  7. This is the matrix after editing the value of one element at a particular index 
  8.  [[1, 2, 345], [234], [4, 5, 6], [7, 8, 9], [1, 2, 3]]


Code 3 :

Input:

  1. a=[[1,2,3],[4,5,6],[7,8,9]]
  2. print("This is Matrix a ",a)
  3. b=[[1,2,3],[4,5,6],[7,8,9]]
  4. print("This is Matrix b ",b)
  5. c=[[0,0,0],[0,0,0],[0,0,0]]
  6. for i in range(len(a)):
  7.     for j in range(len(a[0])):
  8.         c[i][j]=a[i][j]+b[i][j]
  9.         
  10. print("This is Matrix c which has addition of a and b ",c)
Output:


  1. This is Matrix a 
  2.  [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  3. This is Matrix b 
  4.  [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  5. This is Matrix c which has addition of a and b 
  6.  [[2, 4, 6], [8, 10, 12], [14, 16, 18]]
This code shows how to perform addition of 2 matrices. Here we have our two initial matrices namely a and b. We are performing addition and showing the result in a 3rd matrix c. We can do it in the same matrix as any of a or b but for the reason of simplicity and ease of learning we have taken c.

Now, first 'for' loop will be running to check the number of rows in the matrix. The number of rows in the matrix is nothing but the length of list a and b. Hence we have taken the length of the lists a and b by the command len(a).

The second loop runs to check the number of columns. To find the number of columns, here it means the number of elements for each list inside our list. Hence we have used len(a[i]) command to find the length of that particular column.

Keep in mind that this is a symmetrical matrix of same dimensions aka 3x3. This code might need some alterations when performing for non symmetric matrices or random inputs.

We use the combination of first and second 'for' loop to traverse through each and every element in our virtual matrix. Then we add the elements and put the result in a new 3rd matrix c at the position (i,j) aka means at the same position as the elements it's the summation of.

Later we just print the result matrix and it shows the result of our operation. This code needs a bit more of a logical practice to understand about the traversal that we are perfoming here to calculate the results.

Assignment

Similarly, you can perform subtraction, multiplication, and modular division( it gives the remainder), or even usual division ( gives the quotient).


I hope you enjoyed as much as I did writing this chapter. Here you learnt something more about loops, variables as well as functions in a gist and how to go about using them and build your own logic for various things as well as sorting the lists.


~*~*~*~

This series is totally authored by me (Dhawal Joshi). Any similarities found on the text, or codes or anything is purely accidental. All the sources of reference will be mentioned, linked and will be given the proper credits. If I miss anything or there's anything wrong, feel free to comment or send me an email and I'll try to edit it out. I am not a Python expert, I am sharing whatever I have learnt on my own and with a few sources around to refer from which will be mentioned. Also feel free to share this series with others so most can benefit out of it.

~*~*~*~

Do comment and share your thoughts about it! I'd love to know what do you think. Also, I'd keep updating it quite often so do follow the Website to get all the updates by clicking here.

Also, a minor headsup.... Obsessed is free to read on Kindle Unlimited! Do check it out. I'd be glad to read your reviews!
Share :

Are you getting regular fresh content updates? If not click on the button below.

0 comments:

Post a Comment

Do comment and share your thoughts.