How do you multiply a matrix by a scalar in python?
Is there a more 'mathematical' way to do the following: Show
So for what I'm doing is:
asked Mar 2, 2015 at 23:40
David542David542 103k158 gold badges441 silver badges749 bronze badges 2 The mathematical equivalent of what you're describing is the operation of multiplication by a scalar for a vector. Thus, my suggestion would be to convert your list of elements into a "vector" and then multiply that by the scalar. A standard way of doing that would be using Instead of
You can use
Sample Output:
answered Mar 2, 2015 at 23:42
Another approach
answered Mar 2, 2015 at 23:43
levilevi 20.9k7 gold badges65 silver badges71 bronze badges View Discussion Improve Article Save Article View Discussion Improve Article Save Article Given a matrix and a scalar element k, our task is to find out the scalar product of that matrix. Input : mat[][] = {{2, 3} {5, 4}} k = 5 Output : 10 15 25 20 We multiply 5 with every element. Input : 1 2 3 4 5 6 7 8 9 k = 4 Output : 4 8 12 16 20 24 28 32 36 The scalar multiplication of a number k(scalar), multiply it on every entry in the matrix. and a matrix A is the matrix kA. C++
Java
Python 3
C#
PHP
Javascript
Output: Scalar Product Matrix is : 4 8 12 16 20 24 28 32 36 Time Complexity: O(n2), Auxiliary Space: O(1), since no extra space has been taken. How do you multiply a matrix by a number in Python?L L2=other. L result=[] if len(L[0])==len(L2): for i in range(len(L)): row=[] for j in range(len(L2[0])): var=0 for k in range(len(L2)): var=var+L[i][k]*L2[k][j] row=row+[var] result = result+[row] return matrix(result) else: raise ValueError('You may not only multiply m*n * n*q matrices.
How do you multiply a list by a scalar in Python?Use the syntax [element * number for element in list] to multiply each element in list by number .. a_list = [1, 2, 3]. multiplied_list = [element * 2 for element in a_list]. print(multiplied_list). |