Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Create a class called 'Matrix' containing constructor that initializes the number of rows and the number of columns of a new Matrix object. The Matrix class has the following information:
1 - number of rows of matrix
2 - number of columns of matrix
3 - elements of matrix 
The Matrix class has functions for each of the following:
1 - get the number of rows
2 - get the number of columns
3 - set the elements of the matrix at a given position (i,j)
4 - adding two matrices.
5 - multiplying the two matrices
You can assume that the dimensions are correct for the multiplication and addition.


What I have tried:

class Matrix:
    def __init__(self,l,k):
        self.row=l
        self.col=k
    def detail(self):
        matrix=[]
        for i in range(self.row):
            matrix.append([])
            for j in range(self.col):
                self.l=int(input("enter the element"))
                matrix[i].append(self.l)
        print(matrix)
    def col1(self):
        print("No of rows are:",self.row)
    def row1(self):
        print("No of col are:",self.col)
    def sum(self,m,n):
        self.c=m
        self.v=n
        matrix2=[]
        for i in range(self.row):
          for j in range(self.col):
            matrix2[i][j]=self.c[i][j]+self.v[i][j]
        return matrix2


row=int(input("Enter the no of row:"))
col=int(input("Enter the no of col:"))
m1=Matrix(row,col)
m2=Matrix(row,col)
m1.detail()
m1.col1()
m1.row1()
m3=m1.sum(m1,m2)
print(m2)

But it is giving error on 25th line that matrix object is not subscriptable
Posted
Updated 31-Jul-22 1:29am
Comments
Patrice T 31-Jul-22 7:23am    
What issue ?
Abhinav Kabra 31-Jul-22 7:25am    
But it is giving error on 25th line that matrix object is not subscriptable
Patrice T 31-Jul-22 7:37am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
0x01AA 31-Jul-22 10:08am    
I' missing a member variable in this class which holds the values.

1 solution

Python
matrix2=[]
for i in range(self.row):
  for j in range(self.col):
    matrix2[i][j]=self.c[i][j]+self.v[i][j]

You declare matrix2 as a list, but at that point it does not contain any members, or dimensions. You need to append values from the row and col counts, creating internal lists of columns for each row. Something like:
Python
matrix2=[]
for i in range(self.row):
    matrix2.append([])
    for j in range(self.col):
        matrix2[i].append(self.c[i][j]+self.v[i][j])
 
Share this answer
 
Comments
Abhinav Kabra 31-Jul-22 7:37am    
Still it is giving same error when I use this code
Richard MacCutchan 31-Jul-22 7:54am    
Please use the Improve question link above, and show the modified code and the exact error message. We cannot guess what you have done.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900