Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 matrix by vector
#1
This code is supposed to multiply a matrix by a vector without the aid of a .dot function.
In order to make sure I get the right result, I use the @ command right after printing the function.
This code prints
[7, 15, 24]
when using the function
and
[ 6 15 24]
when using the dot product command.
Can anyone please tell me where this is wrong?
Why does the function print 7 instead of 6?

def matvec1(a,b):
    if a.shape[0] == b.shape[0]:
        for i in range(len(a)):
            newarray = []
            s = 0
            for j in range(len(b)):
                s += a[i][j] * b[i]
                newarray.append(s)
        return newarray
    else: 
        return "The matrix and vector aren't compatible."
        
        
    
A1=np.array([[1,2,3],[4,5,6],[7,8,9]    ])
B1=np.array([1,1,1])

print(matvec1(A1,B1))
print(A1@B1)
Quote
#2
You are building newarray wrong. You are resetting it to empty each row, and appending to it after each addition. You should just set it to empty once (before the first for loop), and append to it after calculating each row sum (after the j for loop, it is currently indented under that loop).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
I have done what you said but now I am getting a even stranger result.
I set the empty list before the first loop and I left append after the j loop.
but now what I am getting is
[1, 3, 6, 10, 15, 21, 28, 36, 45].

How come?

def matvec1(a,b):
    newarray = []
    s = 0
    if a.shape[0] == b.shape[0]:
        for i in range(len(a)):
            for j in range(len(b)):
                s += a[i][j] * b[i]
                newarray.append(s)
        return newarray
    else: 
        return "The matrix and vector aren't compatible."
        
        
    
A1=np.array([[1,2,3],[4,5,6],[7,8,9]    ])
B1=np.array([1,1,1])

print(matvec1(A1,B1))
print(A1@B1)

Quote
#4
maybe have a look of the indentation of "newarray.append(s)" line (go back once on the left) and the result is identical to numpy.dot

I've a comment/advice: dynamic allocation memory is costly (using append) and you must initiate your matrix/array before any calculation (using numpy.zeros)
ichabod801 likes this post
Quote
#5
Paul is right, you need to unindent the append.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#6
with your suggestion I get another outcome:
[6, 21, 45]
Quote
#7
Well, you moved s = 0. It should be back where you had it before, between the for i loop and the for j loop.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#8
thank you. Now it works, but I am not understanding how the location of s=0 makes a difference.
Would you perhaps be willing to explain, if not too time consuming ?
Quote
#9
s is the sum of the products in each row in a. Since it is for each row, you need to reset it at the start of each row. The i loop goes through the rows, so it should be reset at the start of each i loop.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  comparing each rows of two matrix PhysChem 10 923 May-17-2019, 06:50 AM
Last Post: Gribouillis
  Inverse of singular matrix Divanova94 4 657 May-11-2019, 02:40 AM
Last Post: scidam
  Extract of matrix subpart using a deep copy paul18fr 2 183 May-02-2019, 06:49 AM
Last Post: paul18fr
  Help in adding confusion matrix Aashish 5 489 Apr-15-2019, 11:45 PM
Last Post: scidam
  counting the occurence of a specified number in a numpy-matrix PhysChem 1 217 Apr-03-2019, 01:37 PM
Last Post: PhysChem
  function matrix mcgrim 14 684 Mar-19-2019, 09:28 PM
Last Post: mcgrim
  PyCM 1.8 released: Machine learning library for confusion matrix statistical analysis sepandhaghighi 0 299 Jan-05-2019, 12:36 PM
Last Post: sepandhaghighi
  Creating matrix counting words in list of strings jazmad 2 433 Dec-23-2018, 05:47 PM
Last Post: jazmad
  Clustering based on a variable and on a distance matrix flucoe 2 571 Dec-16-2018, 09:57 PM
Last Post: flucoe
  The twelfth root of a matrix Arduinol 9 707 Nov-28-2018, 11:36 AM
Last Post: Gribouillis

Forum Jump:


Users browsing this thread: 1 Guest(s)