Hey I was trying to write a program to find the sum of two matrices A and B and store the sum in another matrix C but I am not getting this code of Declaring Matrices with for loop
r = int(input('Enter no. of rows of matrix A'))
c = int(input('Enter the number of colm. of matrix B'))
r1 = int(input('Enter the number of rows of Matrix B'))
c1 = int(input('Enter the number of colm. of Matrix B'))
#declaring of matrices
A = [[0 for x in range(c)]for x in range(r)]
B = [[0 for x in range(c1)]for x in range(r1)]
Related
I have this task: 'Write a program that adds two square matrices. The program will read the dimension of the matrix, N, and will then read N*N numbers representing the first matrix, row by row. It will then read another N*N numbers representing the second matrix. The program will output the resulting matrix, one row per line. ' for which I wrote the code below. However, the platform I am doing the task on keeps saying that 1 of 2 tests failed...It works just fine for me. Maybe the problem is on their side?
from operator import add
#Enter a digit for you matrix, e.g if you want it to be 2x2 enter 2
n = int(input())
#Input digits for both matrixes rows one at a time
matrix1_r1 = [int(input()) for x in range(n)]
matrix1_r2 = [int(input()) for x in range(n)]
matrix2_r1 = [int(input()) for x in range(n)]
matrix2_r2 = [int(input()) for x in range(n)]
final1 = list(map(add, matrix1_r1, matrix2_r1))
final2 = list(map(add, matrix1_r2, matrix2_r2))
print(final1)
print(final2)
Their sample innput is:
2
1
2
3
4
5
6
7
8
their sample output is:
[6, 8]
[10, 12]
Your code works for the example, and for any input that is 2 by 2. It will fail for any other sized matrix, because your code only computes two rows for each matrix. Rather than hard-coding something so fundamental, you should be using nested loops and a list of lists to get the right number of rows. Or, if you want to be a little fancy, list comprehensions can do it all really neatly:
n = int(input())
matrix1 = [[int(input()) for col in range(n)] for row in range(n)]
matrix2 = [[int(input()) for col in range(n)] for row in range(n)]
matrix_sum = [[a + b for a, b in zip(row1, row2)] for row1, row2 in zip(matrix1, matrix2)]
print(*matrix_sum, sep='\n')
I need to perform a matrix multiplication between 2 matrices by taking user input. The below code works fine for the multiplication part but if the no. of rows of 1st matrix are not equal to the no. of columns of the 2nd matrix then it should print NOT POSSIBLE and exit. But it still goes on to add the elements of the matrices. What could possibly be wrong in this code & what could be the solution for the same. Any help would be greatly appreciated
def p_mat(M,row_n,col_n):
for i in range(row_n):
for j in range(col_n):
print(M[i][j],end=" ")
print()
def mat_mul(A_rows,A_cols,A,B_rows,B_cols,B):
if A_cols != B_rows:
print("NOT POSSIBLE")
else:
C = [[0 for i in range(B_cols)] for j in range(A_rows)]
for i in range(A_rows) :
for j in range(B_cols) :
C[i][j] = 0
for k in range(B_rows) :
C[i][j] += A[i][k] * B[k][j]
p_mat(C, A_rows, B_cols)
if __name__== "__main__":
A_rows = int(input("Enter number of rows of 1st matrix: "))
A_cols = int(input("Enter number of columns of 1st matrix: "))
B_rows = int(input("Enter number of rows of 2nd matrix: "))
B_cols = int(input("Enter number of columns of 2nd matrix: "))
##### Initialization of matrix A and B #####
A = [[0 for i in range(B_cols)] for j in range(A_rows)]
B = [[0 for i in range(B_cols)] for j in range(A_rows)]
print("Enter the elements of the 1st matrix: ")
for i in range(A_rows):
for j in range(A_cols):
A[i][j] = int(input("A[" + str(i) + "][" + str(j) + "]: "))
print("Enter the elements of the 2nd matrix: ")
for i in range(B_rows):
for j in range(B_cols):
B[i][j] = int(input("B[" + str(i) + "][" + str(j) + "]:"))
##### Print the 1st & 2nd matrices #####
print("First Matrix : ")
p_mat(A,A_rows,A_cols)
print("Second Matrix : ")
p_mat(B,B_rows,B_cols)
### Function call to multiply the matrices ###
mat_mul(A_rows,A_cols,A,B_rows,B_cols,B)
For matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix.
If you want to check the no of rows of 1st matrix and the no. of columns of the 2nd matrix then change the if A_cols != B_rows to if A_rows != B_cols
With your current code, it will print NOT POSSIBLE when A_cols != B_rows which is right.
Ex.
Enter number of rows of 1st matrix: 2
Enter number of columns of 1st matrix: 3
Enter number of rows of 2nd matrix: 2
Enter number of columns of 2nd matrix: 3
Enter the elements of the 1st matrix:
A[0][0]: 1
A[0][1]: 2
A[0][2]: 3
A[1][0]: 4
A[1][1]: 5
A[1][2]: 6
Enter the elements of the 2nd matrix:
B[0][0]:1
B[0][1]:2
B[0][2]:3
B[1][0]:4
B[1][1]:5
B[1][2]:6
First Matrix :
1 2 3
4 5 6
Second Matrix :
1 2 3
4 5 6
NOT POSSIBLE
Another mistake in the code is when you are initialize the Matrices.You are doing
A = [[0 for i in range(B_cols)] for j in range(A_rows)]
B = [[0 for i in range(B_cols)] for j in range(A_rows)]
If the B_cols are smaller than the A_cols when you adding elements in A it will raise IndexError
The same if the B_cols are greater than A_cols when you are adding elements to B will raise IndexError.
Change it to
A = [[0 for i in range(A_cols)] for j in range(A_rows)]
B = [[0 for i in range(B_cols)] for j in range(B_rows)]
Input: First line contains T, which is the number of test cases. First line of each test case contains two integers N, M and N lines follow which contains M spaced integers.
Input:
1
2 3
1 0 0
8 -9 -1
Output:
-1
The code I've written is:
a = int(input())
for _ in range(a):
b = list(map(int,input().split()))
c = [[int(j) for j in input().split()] for i in range(len(b))]
print(sum(sum(x) if isinstance(x,list) else x for x in c))
I get the output correct. But, I'm unable to submit it because of some arising errors.
I have points A, B and C as in this picture:
link
I have the coordinates of A, B, and C. In this example, A = (1, 1), B = (4.5, 2), and C = (6, 5.5). Points Q1 and Q2 are on the line shown, where the distances from Q1 and Q2 to B are both equal to a number r (in this example, r = 3).
How I can get the coordinates of Q1 and Q2?
Thanks.
Let's assume you have some geometry primitives, such as vector subtraction and vector lengths. Then, you can compute Q2 as follows (in pseudo-code):
r = 3
v = C.minus(B)
w = B.minus(A)
u = v.scaleBy(length(w)) + w.scaleBy(length(v))
Q2 = B.plus(u.scaleBy(r / length(u))
Here, v.scaleBy(5) returns a vector that has the x and y coordinates of v multiplied by 5. The functions length, plus, and minus should be self-explanatory.
In excel, I am doing a simple linear regression of two vector X and Y. When I plot X vs Y and fit with linear equation and the result y = kx + b can be shown in the figure. I need to use k and b for my further calculation. I am wondering if there is any equation that can direct return the value of k and b in excel.
Many thanks in advance for he help!
Assuming x values are in A2:A10 and y in B2:B10, your slope (k) with:
=LINEST(B2:B10,A2:A10)
and the intercept (b) with:
=INTERCEPT(B2:B10,A2:A10)