Scipy Median Filter Which Ignores Zero-Valued Datapoints? - python-3.x

I have been using the median_filter function from scipy.ndimage. I would like to use this in a way that discards any data that isn't positively valued.
That is, suppose one iteration of the filter acts over:
[40, 50, 0, 90]
If I simply run the median filter over this, I will get 45. I desire a function which ignores the zero, such that the median is 50.
Thanks for your suggestions!

In excel we do this by providing an if statement [then is implied.]
=IF(SUM(F38,F43,F47)>60,60,IF(SUM(F38,F43,F47)<34,34,(SUM(F38,F43,F47))))
This would make any answer over 60 equal to 60.
=IF(E19A19=0,"", E19A19) ignores 0.
=IF(E19A19<=0,"", E19A19) returns nothing for 0 or any negative number. I hope this leads you in the right direction. It will depend on your platform but you are looking for eliminating all non positive inputs. something to the effect of
input [n]
If input [n] <= 0 zero, return null.
next input.

Related

How to evaluate a sum in excel to return 0 if the sum of two values is >0 (for my data I do not care about Positive values)

I am summing loads for member design and have the correct values for downforce. However, when I use my formulas for uplift (-) if there is no uplift it returns a positive value which is objectively correct however it is not needed for my purposes. I only need negative values for uplift.
I've tried
sumif(Range,SUM(Range)<0,Sum Range)
sumif(Range,"<0")... this just returns the only negative value which is not what I need
basically, i still need to sum the data but if it returns a positive it should return zero, and if it returns a negative keep the value
Ex.)
2+3 should = 0
3-9 should = -6
You can use MIN to not allow the value to become greater than 0
=MIN(SUM(A1,A2),0)
This will make any positive number 0 because 0 is lower than any positive number.
Something like this?
=IF(SUM(J87+K87)>0,0,J87+K87)

How can I express easily a formula that has a lot of nesting Ifs

I want to express a formula that says if a number in a column is 50 to 99, then return 50. If 100-149, then return 100, 150-199, then return 150, etc, etc. I need a more concise way to do that for numbers that could reach 2000 (in 50 increments).
Right now my formula is =if(and >50 <100),50,if >100,100,true,0) or something like that, I can't see if right now.
There's probably a faster way, but here's what I would do:
Create a new column that rounds down to the nearest 50:
Assume the numbers are in Column A:
=CONCAT(FLOOR(A2,50),"-",IF(FLOOR(A2,100)-1<FLOOR(A2,50),FLOOR(A2,100)+99,FLOOR(A2,100)-1))
This will produce, for every row, the nearest 50 and nearest 100-1. Also, it allows you to go to 10,000, 50,000, 100,000 and never have to change this formula.
The only thing is adding another nested if for any number below 50, but that's up to you. Otherwise, it shows as 0-99 for any number under 50 and 50-99 for any number below 99 but above 50.
Edit
I found out, after all that work, that you just wanted it rounded down to the nearest 50. Just use =FLOOR(A2, 50)
Divide the number by 50, then multiply the integer of that by 50:
=INT(A1/50)*50
Or subtract half the number and use MROUND:
=MROUND(A1-25,50)

Excel formula that if true uses value but if false repeats until true

My formula is
=NORM.INV(RAND(), 0.08, 0.06)
basically generating random values in the normal distribution with probability between 0 and 1, mean 0.08 and standard deviation 0.06. What I need to happen is ensure that my output is greater than or equal to 0.03. Basically, if the output of the formula is greater than or equal to 0.03, fantastic, put that output in the cell. If the output of the formula is less than 0.03, repeat until greater and then use that value etc etc. Cannot seem to work it out
You cannot make the formula work until the desired result is achieved, but you can limit the parameters so that the non-desired result will not achieved. In this case, the RAND should be limited to values greater than 0.2025. You can use RANDBETWEEN:
=NORM.INV(RANDBETWEEN(2025;10^4)/10^4; 0.08; 0.06)

nullif when zero or negative value

I frequently use the nullif when value equals to zero, and wondering if it's possible to also display null when it equals to negative number? My current formula is NULLIF({quantity}-{quantityshiprecv} ,0) but this displays negative numbers when {quantityshiprecv} is greater than {quantity}, and I'm hoping to display null.
You just need to modify the first argument to return 0 if {quantity}-{quantityshiprecv} is negative, but still give the correct answer if it's positve. Here's one way:
NULLIF(({quantity}-{quantityshiprecv} + ABS({quantity}-{quantityshiprecv}))/2,0)
This adds the absolute value of the difference to the unchanged difference. If the difference is positive, that gives you double what you want. If it's negative the absolute (ABS()) will negate that negative value leaving you with zero. You can then divide by 2; positive is now correct, negative remains zero. The rest of the NULLIF() remains the same.

binary sequence subsum combinations

Given a sequence a1a2....a_{m+n} with n +1s and m -1s, if for any 1=< i <=m+n, we have
sum(ai) >=0, i.e.,
a1 >= 0
a1+a2>=0
a1+a2+a3>=0
...
a1+a2+...+a_{m+n}>=0
then the number of sequence that meets the requirement is C(m+n,n) - C(m+n,n-1), where the first item is the total number of sequence, and the second term refers to those sub-sum < 0.
I was wondering whether there is a similar formula for the bi-side sequence number :
a1 >= 0
a1+a2>=0
a1+a2+a3>=0
...
a1+a2+...+a_{m+n}>=0
a_{m+n}>=0
a_{m+n-1}+a_{m+n}>=0
...
a1+a2+...+a_{m+n}>=0
I feel like it can be derived similarly with the single-side subsum problem, but the number C(m+n,n) - 2 * C(m+n,n-1) is definitely incorrect. Any ideas ?
A clue: the first case is a number of paths (with +-1 step) from (0,0) to (n+m, n-m) point, where path never falls below zero line. (Like Catalan numbers for parenthesis pairs, but without balance requirement n=2m)
Desired formula is a number of (+-1) paths which never rise above (n-m) line. It is possible to get recursive formulas. I hope that compact formula exists for it.
If we consider lattice path at nxm grid, where horizontal step for +1 and vertical step for -1, then we need a number of paths restricted by parallelogramm with (n-m) base

Resources