There is a formula in matlab

cvDelta = sdDelta.^2/delta; 

where sdDelta and delta are arrays. As a result of execution we get a scalar (a number, and NOT A MASSIF ) In Python, you need to repeat this formula, but as a result I get an array, instead of a scalar value. Here is my code.

 cvDelta = sdDelta ** 2 / delta 

I suspect that the fact is that in Python, operations with arrays are somehow different.

  • one
    And what does the operation array (vector) / array mean? To get a scalar we need the same size vector and transpose (v1) * v2 - m9_psy
  • one
    use NumPy or ScientificPython - vadim vaduxa
  • @ m9_psy Arrays of the same size. I myself can not fully understand what is happening in Matlabovskom code snippet. - EmptyMan
  • one
    the division operation in the trademark means something completely different - Arnial
  • one
    emulation of matlab division on python in english stackoverflow - Arnial

1 answer 1

What the problem is completely incomprehensible, but here is an example of using numpy :

 import numpy as np A = np.array([2, 2, 2, 2, 2]) B = np.array([5, 5, 5, 5, 5]) print(ATdot(B)) # A и B не вектора размером (5,1), а массивы, поэтому numpy простит несоответствие размеров print(A.dot(B)) >>> 50 >>> 50 A = np.array([[2], [2], [2], [2], [2]]) B = np.array([[5], [5], [5], [5], [5]]) # Не работает из-за несоответствия размеров - нельзя умножить (5,1) на (5,1) print(A.dot(B)) 

In the simplest cases, you can do without numpy , but in terms of speed, the naive solution and the numpy operations are completely incomparable - numpy usually faster at times:

 A = [2, 2, 2, 2, 2] B = [5, 5, 5, 5, 5] assert(len(A) == len(B)) print(sum([A[index] * B[index] for index in range(len(A))])) >>> 50 
  • Appreciate your answer! .dot - scalar product. Please help with my particular example, where the array is raised to a power, and then division into another array. cvDelta = sdDelta ** 2 / delta - we need a scalar result. - EmptyMan
  • one
    @EmptyMan, Vectors have no such operation. There is division by number, but division by vector? How to present it geometrically? You can only imagine element-by-element division. - m9_psy 4:04 pm