I'm trying to use fancy indexing instead of looping to speed up a function in Numpy. To the best of my knowledge, I've implemented the fancy indexing version correctly. The problem is that the two functions (loop and fancy-indexed) do not return the same result. I'm not sure why. It's worth pointing out that the functions do return the same result if a smaller array is used (e.g., 20 x 20 x 20).

Below I've included everything necessary to reproduce the error. If the functions do return the same result, then the line `find_maxdiff(data) - find_maxdiff_fancy(data)`

should return an array full of zeroes.

```
from numpy import *
def rms(data, axis=0):
return sqrt(mean(data ** 2, axis))
def find_maxdiff(data):
samples, channels, epochs = shape(data)
window_size = 50
maxdiff = zeros(epochs)
for epoch in xrange(epochs):
signal = rms(data[:, :, epoch], axis=1)
for t in xrange(window_size, alen(signal) - window_size):
amp_a = mean(signal[t-window_size:t], axis=0)
amp_b = mean(signal[t:t+window_size], axis=0)
the_diff = abs(amp_b - amp_a)
if the_diff > maxdiff[epoch]:
maxdiff[epoch] = the_diff
return maxdiff
def find_maxdiff_fancy(data):
samples, channels, epochs = shape(data)
window_size = 50
maxdiff = zeros(epochs)
signal = rms(data, axis=1)
for t in xrange(window_size, alen(signal) - window_size):
amp_a = mean(signal[t-window_size:t], axis=0)
amp_b = mean(signal[t:t+window_size], axis=0)
the_diff = abs(amp_b - amp_a)
maxdiff[the_diff > maxdiff] = the_diff
return maxdiff
data = random.random((600, 20, 100))
find_maxdiff(data) - find_maxdiff_fancy(data)
data = random.random((20, 20, 20))
find_maxdiff(data) - find_maxdiff_fancy(data)
```

First, in fancy your signal is now 2D if I understand correctly - so I think it would be clearer to index it explicitly (eg amp_a = mean(signal[t-window_size:t,:], axis=0). Similarly with alen(signal) - this should just be samples in both cases so I think it would be clearer to use that.

It is wrong whenever you are actually doing something in the `t`

loop - when `samples < window_lenght`

as in the 20x20x20 example, that loop never gets executed. As soon as that loop is executed more than once (ie `samples > 2 *window_length+1`

) then the errors come. Not sure why though - they do look equivalent to me.

The problem is this line:

```
maxdiff[the_diff > maxdiff] = the_diff
```

The left side selects only some elements of maxdiff, but the right side contains all elements of the_diff. This should work instead:

```
replaceElements = the_diff > maxdiff
maxdiff[replaceElements] = the_diff[replaceElements]
```

or simply:

```
maxdiff = maximum(maxdiff, the_diff)
```

As for why 20x20x20 size seems to work: This is because your window size is too large, so nothing gets executed.

Similar Questions

There is another post here about Atan but I dont see any relevant answers: http://stackoverflow.com/questions/311501/c-why-math-atanmath-tanx-x Isn't Math.Atan the same as tan-1? On my calculator I do

I have code: int SomeClass::sum(int x) { return x+=x; } int SomeClass::sum(int & x) { return x+=x; } .... int num = 0; int result = sum(num); that not work. How I can use both functions and indi

I have two PHP functions to calculate the relation between two texts. They both use the bag of words model but check2() is much faster. Anyway, both functions give the same results. Why? check1() uses

What math is involved and how do I tell if two addresses are in the same 4 kilobyte page?

I have two functions. How would i run two functions at the same time? I Know should use threading. I need a example for Multi Threading . I am using Visual Studio 2010

I want to return date from last week except yesterday and I want to return only yesterday's data. My last week query looks like this SELECT b.id, b.publisher_id, b.name, b.size, b.present, b.display,

I have two interfaces that declare functions with the same name but incompatible signatures: interface IDog { function bark() : void; } interface IAdvancedDog { function bark(volume : Number) : void;

So noticed from this page that none of the math functions in c++11 seems to make use of constexpr, whereas I believe all of them could be. So that leaves me with two questions, one is why did they cho

It recently came to my attention that member functions completely shadow free functions with the same name when inside the class. And by completely i mean that every free function with the same name i

If I multiplie two 16-bit numbers, the result will be 32-bit long. But why is this so? What is the clear explanation for this? And for my right understanding: The calculation for this is: n-bit number

Hi i want to see if it is possible to create two onload functions at the same time. e.g <body onload=alert('test')> <script> alert(test) </script> This does not result 2 alert

I am using linq to search product in ObservableCollection, but it does not return any result, for example I have a product name stack, but when I search s, it return no result: private Observable

Bizzarely, every function from Python's math module seems to work just fine with Decimal objects. For example: frexp, exp, cos. When I type print(math.frexp(decimal.Decimal('2341.12412'))), Python pri

alert(Math.cos(Math.PI/2)); Why the result is not exact zero? Is this inaccurancy, or some implementation error?

I wanted to replace one line using sed, but got two same lines: $ sed '50s/false/true/p' ./src/com/android/quicksearchbox/SearchableSource.java >SearchableSource.java.1 $ diff SearchableSource.java

Is there a way to join 2 subdocuments in a result? Example: I have publishers and the publishes have packages and books for sell. What i want is to show only the packages they want to sell. Here is my

I am new to JAVA, and I am trying to learn the language; please excuse me if I am being silly. So I was testing out Math.Pow( ) and came across that when I use a division function in the second argum

I am using libsvm in matlab and I am trying to call svmtrain. However I know that Matlab also has a built-in function called svmtrain. I believe they take the same parameters in different order. How d

This question already has an answer here: Why does Double.NaN==Double.NaN return false? 7 answers I was looking at the Math sources in the Android Framework and here is what the round() method

I am a beginning programmer and know little about C. One thing I know but have not had explained to me is why functions can't return arrays. This is easily circumvented (Return a 2d array from a funct

I have two statements that return the same result, but produce different execution plans. First >>> Index Seek Second >>> Index Scan Can anyone explain why? Example CREATE TABLE

Why can't we use both return and yield return in the same method? For example, we can have GetIntegers1 and GetIntegers2 below, but not GetIntegers3. public IEnumerable<int> GetIntegers1() { ret

I have two functions here: public function bindOwnerToSites(){ error_reporting (E_ALL^ E_NOTICE); foreach( $this->balance as $keysaa =>$key_valuesa)//getsitebalance { foreach( $this->sites as

Can we have two main() functions in a C++ program?

from http://docs.python.org/2/library/math.html: math.frexp(x) Return the mantissa and exponent of x as the pair (m, e). m is a float and e is an integer such that x == m * 2**e exactly. If x is zero

I don't understand why it has two difference result. Please help me, to explain these results. This is my codesorce: # coding: iso-8859-2 class Satelite: def __init__(self, name, mass=100, speed=0): s

Suppose we have a number of filter functions that accept the same parameters and return a boolean result. let filter1 _ _ = true let filter2 _ _ = false These can be combined into a single filter. le

First, my math is not strong! But I was calculating the angle of a point from the origin (0, 0) to (-1, -1). This angle is 225° or 5π/4. 5π/4 = 3.9269908169872415480783042290994 Math.Atan2(-1, -1) =

I found out that different computer return different result from the following statement in Java. private static final Date ORIGIN = new Date(0L); In my computer, it return the following result. W

In C#, why don't the rounding math functions Floor, Ceiling and Round return an int? Considering the result of the function will always be an integer, why does it return a float, double or decimal?

So I'm learning about socket programming and have wrote a nifty little chat server. The problem I am having is that my client cannot read and write at the same time. I'm not too sure how to set this u

To create a opencl application the fist step is to get platforms by using clGetPlatformIDs i have a problem with the platforms that return from function ;the function return that i have 2 platforms b

I've been doing some reading on password hashing lately, and I've found that PHP has two password hashing functions: password_hash() and crypt(). crypt() looks more versatile and configurable and is w

>>> import math >>> math.sin(68) -0.897927680689 sin(68)=0.927(3dp) Any ideas about why I am getting this result? Thanks.

In the following code, the last two calls to Ceil give unexpected result. Could you help to comment on the reason? Furthermore, if the error (or deviation) is random, could I get the expected value?

Is their a method to have multiple right answers with the same result? function check(code){ if(code == (8 || 9 || 13 || 16 || 17 || 18 || 20 || 32)){ return true; } } I know I can use a switch stat

How would I include two functions with the same name in a php file? I have two files: file1.php which includes function getResidentCount() {} function getResidentData() {} file2.php also includes

In both OS X and iOS, Apple is using the CGFloat typedef to automatically get a float on 32-bit systems and a double on 64-bit systems. But when using the normal Unix math functions you need to make t

I thought that result of any mathematical operation on a NaN should give me a NaN back, but Math.round(Float.NaN) == 0 What is the rationale for such behavior of Math.round()? Curiously, C# behaves di

Why does the following code alert 2? var f = (function x(){ return 1; }, function y(){ return 2; })(); alert(f); What I can see is that somehow the y function is getting executed and x function is ig

In my functions.php I have a function that is adding scripts to the single post sites. Unfortunately it adds it also to the Nothing Found search result page and that's the reason why I get following

Consider the following two programs that perform the same computations in two different ways: // v1.c #include <stdio.h> #include <math.h> int main(void) { int i, j; int nbr_values = 8192;

I'm new to JNI. I have two (or more) Activities that want to use the same JNI function. Currently I have to define two JNI functions that both call another function, e.g. int foo() { return 1; } int

I have two computers with python 2.7.2 (MSC v.1500 32 bit (Intel)] on win32) and numpy 1.6.1. But numpy.mean(data) returns 1.13595094681 on my old computer and 1.13595104218 on my new computer wh

I have a list which has a number of objects from class X. I add one object via clone function, it gets its own index, but if I add one more object using the clone, the object receives the same index a

ticketPriceInPence = 7360 percentageToRefund = 100 (int)(Math.Round((ticketPriceInPence * 0.01) * (percentageToRefund * 0.01), 2, MidpointRounding.AwayFromZero) * 100) This results in : 73.59 (int)(M

In the Model class I use return $query->row(); to return single rows and return $query->result(); when returning multiple rows. On a single page I have to return single rows and multiple rows f

I have two strings, which, when both outputted with echo, produce the same result: x+3=7\\\\x+3-3=7-3\\\\\\thereforex=4 Yes, it's a bit of LaTeX. But when I use: $foo1 == $foo2 PHP takes it as fals

What range of Numbers? Seriously, I got headache trying to figure it out -_- public function gerRandom(i:uint):uint { return Math.round(Math.random()*i); } Whole numbers from 0 to i including? I need

I have two tables: Projects and ProjectsData and I want to execute query with join and get the result in the View. In the Controller I have this code: ViewBag.projectsData = (from pd in db.ProjectsDat