Python Forum

Full Version: row multiplication using vectorization
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear All

Coming from matlab, I'm progessively moving to Numpy/Python; I'm looking if the following calculation can be done (I looked tuto and so on with no sucess so far):

(Matlab code)
Quote:a = (1:5)'
b = (6:10)'
c = a .* b

Note the dot "." in front of the "*" that uses vectorization and provides a new vector [6, 14, 24, 36, 50].

Does somebody ever experienced such vectorization?

Thanks for your feedback

Paul
Moving from matlab environment to numpy/scipy/matplotlib one is pretty simple.
Your example could be rewritten as follows:

import numpy as np
a = np.arange(1, 6)
b = np.arange(6, 11)
c = a * b # element-wise product (in matlab .*)
Or may be I misunderstood something here...
Thanks for the answer

Humm of course I did it, but I got a number as a result (130 here); restarting Spyder fixed the issue and I'm wondering if the issue does not come from it ...

Is there any keyword to force to "clear" all the variables and other inputs prior to run a code?

Paul
Could you provide a piece of code you used?

My code snippet leads to the following result:
Output:
>>> c array([ 6, 14, 24, 36, 50])
I did exactly what you proposed; I failed at the beginning but now it works; I guess the issue did not come from Python or Numpy, but from the Idle itself.

Thanks for your support