Pattern Matching and auto-complete for two arrays - arrays

Let's say i have an array:
[0, -2, 0, 0, -3, 0, -1, 0, 1, 0, 3, 0, 2, 0, 0, 0]
And I want to see if it can fit the pattern below:
[1, 1, 1 , 0, 0 , 0 , 0, 0, 1, 1, 1, 0, 0, 0, 0, 0];
Where 1 means that the number is unique and 0 means that all of those numbers match. Here we would have a match, if I arranged like so:
[-3, -2, -1, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0];
Is there any Matlab function for this or must I think up my own way?

For me it feels like an interpolation problem, so I think the following will be a robust solution:
%// data
A = [0, -2, 0, 0, -3, 0, -1, 0, 1, 0, 3, 0, 2, 0, 0, 0]
B = [1, 1, 1 , 0, 0 , 0 , 0, 0, 1, 1, 1, 0, 0, 0, 0, 0]
%// input data for interpolation
x = 1:numel(A)
v = A;
%// query points
mask = logical(B) & ~logical(A);
xq = find( mask )
%// interpolation
vq = interp1(x(~mask),v(~mask),xq,'linear','extrap')
%// output
out = A.*B;
out(xq) = vq
out =
-3 -2 -1 0 0 0 0 0 1 2 3 0 0 0 0 0

As you said in the comments that you can put any unique numbers where 1s are, and all numbers, that are not zeros, are always unique, so I think following will do it:
A = [0, -2, 0, 0, -3, 0, -1, 0, 1, 0, 3, 0, 2, 0, 0, 0];
B = [1, 1, 1 , 0, 0 , 0 , 0, 0, 1, 1, 1, 0, 0, 0, 0, 0];
B(B==1)= A(A~=0)
which gives this:
B =
-2 -3 -1 0 0 0 0 0 1 3 2 0 0 0 0 0
to match the exact output of yours in the question, you can use the following:
A = [0, -2, 0, 0, -3, 0, -1, 0, 1, 0, 3, 0, 2, 0, 0, 0];
B = [1, 1, 1 , 0, 0 , 0 , 0, 0, 1, 1, 1, 0, 0, 0, 0, 0];
A=sort(A);
B(B==1)= A(A~=0)
which gives this:
B =
-3 -2 -1 0 0 0 0 0 1 2 3 0 0 0 0 0

Assuming:
ind = logical([1, 1, 1 , 0, 0 , 0 , 0, 0, 1, 1, 1, 0, 0, 0, 0, 0]);
You can write:
B = zeros(size(ind)); % only if the zeros at the end of 'B' are important
B(ind) = nonzeros(A)
So you use ind as logical index to order the elements that are not 0 in A, to get:
B =
-2 -3 -1 0 0 0 0 0 1 3 2 0 0 0 0 0
if you also want it sorted (as in your example), replace the last line with:
B(ind) = nonzeros(sort(A))

Related

Use the WHERE method to replace all numbers in a Numpy array with a - 1

I'm trying to use the where method to replace all odd numbers from the below array with a -1
np.array([0, 1, 0, 3, 0, 5, 0, 7, 0, 9])
I've tried using the below, but it's not working.
np.where(Q9 % 2 == 1) = - 1
Thanks for any assistance!
where method only returns indices
arr = np.array([0, 1, 0, 3, 0, 5, 0, 7, 0, 9])
arr[np.where(arr%2!=0)] = -1
print(arr)
output:
[ 0 -1 0 -1 0 -1 0 -1 0 -1]
If you want to replace in the original array, where is not needed, use simple indexing:
a = np.array([0, 1, 0, 3, 0, 5, 0, 7, 0, 9])
a[a%2==1] = -1
a
For a new array:
b = np.where(a%2==1, -1, a)
output: array([ 0, -1, 0, -1, 0, -1, 0, -1, 0, -1])

How to pad a one hot array of length n with a 1 on either side of the 1

For example I have the following array:
[0, 0, 0, 1, 0, 0, 0]
what I want is
[0, 0, 1, 1, 1, 0, 0]
If the 1 is at the at the end, for example [1, 0, 0, 0] it should add only on one side [1, 1, 0, 0]
How do I add a 1 on either side while keeping the array the same length? I have looked at the numpy pad function, but that didn't seem like the right approach.
One way using numpy.convolve with mode == "same":
np.convolve([0, 0, 0, 1, 0, 0, 0], [1,1,1], "same")
Output:
array([0, 0, 1, 1, 1, 0, 0])
With other examples:
np.convolve([1,0,0,0], [1,1,1], "same")
# array([1, 1, 0, 0])
np.convolve([0,0,0,1], [1,1,1], "same")
# array([0, 0, 1, 1])
np.convolve([1,0,0,0,1,0,0,0], [1,1,1], "same")
# array([1, 1, 0, 1, 1, 1, 0, 0])
You can use np.pad to create two shifted copies of the array: one shifted 1 time toward the left (e.g. 0 1 0 -> 1 0 0) and one shifted 1 time toward the right (e.g. 0 1 0 -> 0 0 1).
Then you can add all three arrays together:
0 1 0
1 0 0
+ 0 0 1
-------
1 1 1
Code:
output = a + np.pad(a, (1,0))[:-1] + np.pad(a, (0,1))[1:]
# (1, 0) says to pad 1 time at the start of the array and 0 times at the end
# (0, 1) says to pad 0 times at the start of the array and 1 time at the end
Output:
# Original array
>>> a = np.array([1, 0, 0, 0, 1, 0, 0, 0])
>>> a
array([1, 0, 0, 0, 1, 0, 0, 0])
# New array
>>> output = a + np.pad(a, (1,0))[:-1] + np.pad(a, (0,1))[1:]
>>> output
array([1, 1, 0, 1, 1, 1, 0, 0])

How do I figure out why this multipolygon is invalid in Snowflake?

Run this in Snowflake to see that it returns an error.
with a as ( select to_geography('MULTIPOLYGON (((-75.567446994279891 39.5086159918784 0 0, -75.562456 39.51265 0 0, -75.560317002808333 39.515950000160686 0 0, -75.561742999999993 39.520534 0 0, -75.565546 39.514849999999996 0 0, -75.567446994279891 39.5086159918784 0 0)), ((-75.571759009184774 39.623583997285259 0 0, -75.5712463122867 39.622342955203095 0 0, -75.567694 39.613744 0 0, -75.561934 39.605216 0 0, -75.555869991521988 39.605824005226609 0 0, -75.556733999999992 39.606688 0 0, -75.557502 39.609184 0 0, -75.556878 39.612144 0 0, -75.558446 39.617295999999996 0 0, -75.559614 39.624207999999996 0 0, -75.559102 39.629056 0 0, -75.559446 39.629812 0 0, -75.563328 39.629901 0 0, -75.570798 39.626768 0 0, -75.571299098592291 39.6251077524268 0 0, -75.571759009184774 39.623583997285259 0 0)), ((-75.580363009752176 39.599454999369712 0 0, -75.579291 39.597291999999996 0 0, -75.57916 39.594705 0 0, -75.578007 39.591878 0 0, -75.575426 39.588119999999996 0 0, -75.572118 39.58586 0 0, -75.568646 39.585156 0 0, -75.567689 39.58573 0 0, -75.566625 39.587123 0 0, -75.564604004267125 39.589337004109083 0 0, -75.565823 39.590607999999996 0 0, -75.570774 39.594091 0 0, -75.57287 39.594454 0 0, -75.573681999999991 39.595718 0 0, -75.575760872574207 39.5968808119757 0 0, -75.577721260059008 39.5979773496244 0 0, -75.580363009752176 39.599454999369712 0 0)), ((-75.788595997316847 39.722198993930377 0 0, -75.78857321813 39.7197154361476 0 0, -75.788395 39.700286999999996 0 0, -75.788395 39.700030999999996 0 0, -75.7884665379611 39.6951022248818 0 0, -75.788479176698289 39.6942314495293 0 0, -75.788658 39.681911 0 0, -75.7886469376544 39.681603098048292 0 0, -75.78861599999999 39.680741999999995 0 0, -75.788633391921593 39.6714120622458 0 0, -75.788655907550691 39.659333499406 0 0, -75.788658 39.658211 0 0, -75.788431434671409 39.654318127515495 0 0, -75.788430900679188 39.6543089523983 0 0, -75.7881295637419 39.6491313452212 0 0, -75.7880955854847 39.6485475267544 0 0, -75.787787176812 39.6432484121769 0 0, -75.787541702471188 39.6390306427924 0 0, -75.78754100266589 39.6390186186539 0 0, -75.787449999999993 39.637454999999996 0 0, -75.787202839355288 39.634418454936196 0 0, -75.7871896545333 39.6342564699798 0 0, -75.78689 39.630575 0 0, -75.786216419654011 39.6217124085307 0 0, -75.7854092147263 39.611091659946794 0 0, -75.7850543631961 39.606422723028494 0 0, -75.785053894501687 39.606416556210895 0 0, -75.7843181210525 39.596735662857796 0 0, -75.78384245023949 39.5904770537489 0 0, -75.783842208850487 39.590473877688893 0 0, -75.782823521373089 39.5770705606221 0 0, -75.782460041179007 39.5722880925958 0 0, -75.7824575074535 39.5722547552599 0 0, -75.780785999999992 39.550262 0 0, -75.7796634388864 39.5365036098544 0 0, -75.7795851648761 39.5355442639815 0 0, -75.7795846104508 39.5355374688062 0 0, -75.7795181992453 39.5347235164389 0 0, -75.7762226834811 39.494332849895 0 0, -75.775129622280389 39.4809360180705 0 0, -75.7738556911832 39.465322399495996 0 0, -75.7722345420767 39.4454531900426 0 0, -75.769830558762592 39.415989368484894 0 0, -75.7666931907181 39.377537 0 0, -75.766667 39.377216 0 0, -75.765847374248 39.3671747047822 0 0, -75.7643882360415 39.349298695569395 0 0, -75.7604691694169 39.3012859228405 0 0, -75.7601316135502 39.297150501153595 0 0, -75.760104352373 39.2968165224723 0 0, -75.75 39.297474 0 0, -75.746048 39.297531 0 0, -75.714901 39.299366 0 0, -75.685188 39.297089 0 0, -75.683117 39.294578 0 0, -75.681384999999992 39.293205 0 0, -75.680261 39.292496 0 0, -75.678041999999991 39.292477 0 0, -75.6752 39.290498 0 0, -75.673327 39.291027 0 0, -75.670301999999992 39.290890999999995 0 0, -75.669276 39.291396999999996 0 0, -75.66726899999999 39.290417 0 0, -75.665810999999991 39.290566999999996 0 0, -75.660476 39.291672 0 0, -75.654461 39.291184 0 0, -75.652947 39.291948999999995 0 0, -75.65135 39.291588999999995 0 0, -75.64917899999999 39.293151 0 0, -75.647686 39.29588 0 0, -75.646293 39.29687 0 0, -75.6427 39.299107 0 0, -75.641928 39.300891 0 0, -75.640875 39.301224 0 0, -75.633937 39.301593 0 0, -75.63225 39.301327 0 0, -75.627530999999991 39.304438 0 0, -75.626132 39.30413 0 0, -75.62404699999999 39.306376 0 0, -75.623263999999992 39.308029 0 0, -75.62137899999999 39.308755 0 0, -75.619631 39.310058 0 0, -75.61605999999999 39.309734999999996 0 0, -75.614256 39.308307 0 0, -75.612008 39.308954 0 0, -75.610863 39.309276 0 0, -75.609385 39.308800999999995 0 0, -75.606597999999991 39.30809 0 0, -75.603836 39.311181999999995 0 0, -75.603445999999991 39.309094 0 0, -75.602031 39.30811 0 0, -75.599571 39.308442 0 0, -75.599218 39.309737 0 0, -75.597106 39.309554 0 0, -75.596609 39.309647 0 0, -75.594841 39.309903999999996 0 0, -75.59388899999999 39.308417 0 0, -75.592338 39.309948 0 0, -75.592056 39.311251999999996 0 0, -75.590409 39.311746 0 0, -75.58749499999999 39.311009 0 0, -75.587324999999993 39.309698999999995 0 0, -75.58476499999999 39.308644 0 0, -75.581144999999992 39.311130999999996 0 0, -75.57462 39.313434 0 0, -75.57095799999999 39.318844999999996 0 0, -75.56871599957131 39.323523001317263 0 0, -75.569820001173056 39.326576996779295 0 0, -75.569007 39.328744 0 0, -75.56480599999999 39.331936 0 0, -75.562733999999992 39.332104 0 0, -75.558843 39.333334 0 0, -75.556204999999991 39.335325 0 0, -75.555681007372513 39.33653000312443 0 0, -75.55631799999999 39.337879 0 0, -75.55833399521066 39.339230004872931 0 0, -75.557357995926381 39.341500995668405 0 0, -75.557846 39.342763999999995 0 0, -75.559981999999991 39.343466 0 0, -75.56051899792655 39.345066999353946 0 0, -75.5588 39.346548999999996 0 0, -75.557378 39.349101 0 0, -75.55619 39.348287 0 0, -75.555886 39.346743 0 0, -75.554223 39.345403999999995 0 0, -75.551599 39.345105 0 0, -75.551039996261352 39.345567994418367 0 0, -75.551096 39.349483 0 0, -75.552241999999993 39.350156999999996 0 0, -75.553840999999991 39.349938 0 0, -75.554683007400541 39.351506002990355 0 0, -75.553369 39.35301 0 0, -75.551626 39.35394 0 0, -75.549645 39.353854999999996 0 0, -75.548131 39.352920999999995 0 0, -75.54811 39.351713 0 0, -75.545216 39.350763 0 0, -75.544056 39.351363 0 0, -75.543263 39.353235999999995 0 0, -75.541586 39.354366 0 0, -75.539867 39.353887 0 0, -75.53899 39.351954 0 0, -75.535849 39.350617 0 0, -75.534483994047335 39.351388997582582 0 0, -75.53427799305544 39.353332004157252 0 0, -75.531914 39.354594999999996 0 0, -75.528696 39.353429999999996 0 0, -75.527258995814222 39.354675001898435 0 0, -75.528673 39.356781999999995 0 0, -75.52845300020698 39.358305006725651 0 0, -75.525877 39.359642 0 0, -75.523254 39.359711999999995 0 0, -75.521419999999992 39.358607 0 0, -75.519628 39.360065999999996 0 0, -75.516897 39.360988 0 0, -75.515975 39.364363 0 0, -75.51237208219473 39.365656144406771 0 0, -75.512996 39.366153 0 0, -75.515796999999992 39.378347 0 0, -75.52126299999999 39.381654 0 0, -75.521682 39.387871 0 0, -75.523583 39.391583 0 0, -75.535977 39.409383999999996 0 0, -75.538512 39.416502 0 0, -75.555889999999991 39.430351 0 0, -75.571829994645924 39.438896995945825 0 0, -75.5714550448173 39.4404895611251 0 0, -75.5709849993434 39.442485993390129 0 0, -75.573077 39.445373 0 0, -75.578914 39.44788 0 0, -75.5795930686755 39.4494326149262 0 0, -75.580185 39.450786 0 0, -75.589439 39.460812 0 0, -75.589901 39.462022 0 0, -75.592328999999992 39.467577999999996 0 0, -75.593068 39.477996 0 0, -75.593068000877849 39.47918599762091 0 0, -75.587813999749756 39.49032700194055 0 0, -75.587729 39.495369 0 0, -75.587729 39.4957407450199 0 0, -75.587728998315711 39.4963530034779 0 0, -75.586554401533689 39.497688711813 0 0, -75.581140859129292 39.5038448006784 0 0, -75.576436 39.509195 0 0, -75.574502999999993 39.512516999999995 0 0, -75.5728617838115 39.5160342206675 0 0, -75.572364001474554 39.5171009984447 0 0, -75.570362 39.527223 0 0, -75.569418 39.539124 0 0, -75.569358999999992 39.540589 0 0, -75.565636001056717 39.558509006016514 0 0, -75.564649 39.559922 0 0, -75.5641182880885 39.5606837276848 0 0, -75.563034004787326 39.562239998169261 0 0, -75.570782999999992 39.56728 0 0, -75.571598999999992 39.567727999999995 0 0, -75.586016 39.578448 0 0, -75.586608 39.57888 0 0, -75.58702738490949 39.5798547865464 0 0, -75.58720000000001 39.580256 0 0, -75.587744 39.580672 0 0, -75.591984 39.583248 0 0, -75.592224 39.583568 0 0, -75.597173322751 39.5877820945208 0 0, -75.598747 39.589121999999996 0 0, -75.600785536192 39.5890537256847 0 0, -75.603584 39.58896 0 0, -75.60464 39.58992 0 0, -75.611873 39.597408 0 0, -75.611905 39.597567999999995 0 0, -75.613792996204168 39.606191997215134 0 0, -75.613473001357889 39.606832002184781 0 0, -75.613476995906737 39.606860998404279 0 0, -75.6134736873228 39.606868426370596 0 0, -75.613233 39.607408 0 0, -75.611343991850447 39.610182999893205 0 0, -75.61366500749152 39.612559994452383 0 0, -75.613344994092387 39.613056004804442 0 0, -75.614273 39.61464 0 0, -75.614929004901924 39.615951994615266 0 0, -75.614065 39.61832 0 0, -75.613377 39.620287999999995 0 0, -75.613153 39.62096 0 0, -75.611969 39.621967999999995 0 0, -75.6004949488643 39.6353936845212 0 0, -75.5917467759147 39.6456298433782 0 0, -75.587147 39.651012 0 0, -75.5830124717269 39.6522635452232 0 0, -75.569037 39.656493999999995 0 0, -75.562246 39.656712 0 0, -75.5613567608041 39.6577726838033 0 0, -75.5568448905546 39.6631544390364 0 0, -75.5565726400012 39.663479179279292 0 0, -75.5537715895688 39.666820269999704 0 0, -75.5508077933438 39.6703554837718 0 0, -75.550803870881907 39.670360162481195 0 0, -75.5507962726737 39.670369225617904 0 0, -75.5349831512427 39.689231104242 0 0, -75.5319118207207 39.692894584837696 0 0, -75.52553303088601 39.7005032002623 0 0, -75.512682348589891 39.7158314838503 0 0, -75.509113 39.720089 0 0, -75.4964817737103 39.743315194477894 0 0, -75.490242999999992 39.754787 0 0, -75.4897514259047 39.755503457769095 0 0, -75.4719915820485 39.7813880156216 0 0, -75.4697455653652 39.7846615325088 0 0, -75.4690045587194 39.7857415323977 0 0, -75.468502 39.786474 0 0, -75.446497431828192 39.7963039695592 0 0, -75.4234159950776 39.806614995941658 0 0, -75.428038 39.809211999999995 0 0, -75.4339085190138 39.8117473186932 0 0, -75.4370683549011 39.8131119665163 0 0, -75.437307916589788 39.8132154267429 0 0, -75.449874383721593 39.8186425446778 0 0, -75.45374 39.820312 0 0, -75.454997184031086 39.820770300605005 0 0, -75.4598175446831 39.8225275407136 0 0, -75.4599264483802 39.822567241051004 0 0, -75.463341 39.823812 0 0, -75.463456872955192 39.8238463068355 0 0, -75.472547501842 39.826537795193694 0 0, -75.472677629082384 39.826576322336 0 0, -75.4754890471753 39.8274087068895 0 0, -75.481242 39.829111999999995 0 0, -75.481244921815687 39.8291126972119 0 0, -75.4930772205643 39.831936153535004 0 0, -75.498843 39.833312 0 0, -75.4989861331738 39.833333899718795 0 0, -75.518444 39.836310999999995 0 0, -75.5188176150883 39.836344961758094 0 0, -75.539346 39.838211 0 0, -75.5400582754622 39.838229220042 0 0, -75.5408864120625 39.838250403817796 0 0, -75.5513215785919 39.8385173359007 0 0, -75.570464 39.839006999999995 0 0, -75.5793939947411 39.8385493199286 0 0, -75.579849 39.838526 0 0, -75.5798996512343 39.8385220738603 0 0, -75.5930817368867 39.8375002881084 0 0, -75.593666 39.837455 0 0, -75.59484583814249 39.8372862097586 0 0, -75.595756 39.837156 0 0, -75.617251 39.833999 0 0, -75.634706 39.830163999999996 0 0, -75.634812797602493 39.8301357641688 0 0, -75.641517999999991 39.828362999999996 0 0, -75.662821999999991 39.821149999999996 0 0, -75.667142678086009 39.819267244337 0 0, -75.680794056787789 39.813318592285796 0 0, -75.6815568950873 39.812986181932693 0 0, -75.685991 39.811054 0 0, -75.701208 39.802606 0 0, -75.705787295543 39.7995238880071 0 0, -75.716968999999992 39.791998 0 0, -75.7193345673559 39.7901506045411 0 0, -75.7233991238184 39.7869763794942 0 0, -75.724866264384787 39.785830612575595 0 0, -75.727049 39.784126 0 0, -75.736488999999992 39.775759 0 0, -75.7393235590979 39.77292479948 0 0, -75.7393766862116 39.772871679087004 0 0, -75.744394 39.767855 0 0, -75.751436564661 39.759552050150596 0 0, -75.75306599999999 39.757630999999996 0 0, -75.7544659060403 39.7556311342282 0 0, -75.7592137517567 39.748848497490393 0 0, -75.760101611338087 39.7475801266599 0 0, -75.760346 39.747231 0 0, -75.7658791814134 39.7381059004001 0 0, -75.766058 39.737811 0 0, -75.773786 39.7222 0 0, -75.788595997316847 39.722198993930377 0 0)))') as polygon ), b as ( select to_geography('POINT(-75.677 39.708)') point ) select ST_ASWKT(point), ST_ASWKT(polygon), st_intersects(polygon, point) intersects from a, b
The problem with that string are the multiple zeroes. If you remove them, then it becomes a valid WKT shape:
with a as (
select to_geography(
replace('MULTIPOLYGON (((-75.567446994279891 39.5086159918784 0 0, -75.562456 39.51265 0 0, -75.560317002808333 39.515950000160686 0 0, -75.561742999999993 39.520534 0 0, -75.565546 39.514849999999996 0 0, -75.567446994279891 39.5086159918784 0 0)), ((-75.571759009184774 39.623583997285259 0 0, -75.5712463122867 39.622342955203095 0 0, -75.567694 39.613744 0 0, -75.561934 39.605216 0 0, -75.555869991521988 39.605824005226609 0 0, -75.556733999999992 39.606688 0 0, -75.557502 39.609184 0 0, -75.556878 39.612144 0 0, -75.558446 39.617295999999996 0 0, -75.559614 39.624207999999996 0 0, -75.559102 39.629056 0 0, -75.559446 39.629812 0 0, -75.563328 39.629901 0 0, -75.570798 39.626768 0 0, -75.571299098592291 39.6251077524268 0 0, -75.571759009184774 39.623583997285259 0 0)), ((-75.580363009752176 39.599454999369712 0 0, -75.579291 39.597291999999996 0 0, -75.57916 39.594705 0 0, -75.578007 39.591878 0 0, -75.575426 39.588119999999996 0 0, -75.572118 39.58586 0 0, -75.568646 39.585156 0 0, -75.567689 39.58573 0 0, -75.566625 39.587123 0 0, -75.564604004267125 39.589337004109083 0 0, -75.565823 39.590607999999996 0 0, -75.570774 39.594091 0 0, -75.57287 39.594454 0 0, -75.573681999999991 39.595718 0 0, -75.575760872574207 39.5968808119757 0 0, -75.577721260059008 39.5979773496244 0 0, -75.580363009752176 39.599454999369712 0 0)), ((-75.788595997316847 39.722198993930377 0 0, -75.78857321813 39.7197154361476 0 0, -75.788395 39.700286999999996 0 0, -75.788395 39.700030999999996 0 0, -75.7884665379611 39.6951022248818 0 0, -75.788479176698289 39.6942314495293 0 0, -75.788658 39.681911 0 0, -75.7886469376544 39.681603098048292 0 0, -75.78861599999999 39.680741999999995 0 0, -75.788633391921593 39.6714120622458 0 0, -75.788655907550691 39.659333499406 0 0, -75.788658 39.658211 0 0, -75.788431434671409 39.654318127515495 0 0, -75.788430900679188 39.6543089523983 0 0, -75.7881295637419 39.6491313452212 0 0, -75.7880955854847 39.6485475267544 0 0, -75.787787176812 39.6432484121769 0 0, -75.787541702471188 39.6390306427924 0 0, -75.78754100266589 39.6390186186539 0 0, -75.787449999999993 39.637454999999996 0 0, -75.787202839355288 39.634418454936196 0 0, -75.7871896545333 39.6342564699798 0 0, -75.78689 39.630575 0 0, -75.786216419654011 39.6217124085307 0 0, -75.7854092147263 39.611091659946794 0 0, -75.7850543631961 39.606422723028494 0 0, -75.785053894501687 39.606416556210895 0 0, -75.7843181210525 39.596735662857796 0 0, -75.78384245023949 39.5904770537489 0 0, -75.783842208850487 39.590473877688893 0 0, -75.782823521373089 39.5770705606221 0 0, -75.782460041179007 39.5722880925958 0 0, -75.7824575074535 39.5722547552599 0 0, -75.780785999999992 39.550262 0 0, -75.7796634388864 39.5365036098544 0 0, -75.7795851648761 39.5355442639815 0 0, -75.7795846104508 39.5355374688062 0 0, -75.7795181992453 39.5347235164389 0 0, -75.7762226834811 39.494332849895 0 0, -75.775129622280389 39.4809360180705 0 0, -75.7738556911832 39.465322399495996 0 0, -75.7722345420767 39.4454531900426 0 0, -75.769830558762592 39.415989368484894 0 0, -75.7666931907181 39.377537 0 0, -75.766667 39.377216 0 0, -75.765847374248 39.3671747047822 0 0, -75.7643882360415 39.349298695569395 0 0, -75.7604691694169 39.3012859228405 0 0, -75.7601316135502 39.297150501153595 0 0, -75.760104352373 39.2968165224723 0 0, -75.75 39.297474 0 0, -75.746048 39.297531 0 0, -75.714901 39.299366 0 0, -75.685188 39.297089 0 0, -75.683117 39.294578 0 0, -75.681384999999992 39.293205 0 0, -75.680261 39.292496 0 0, -75.678041999999991 39.292477 0 0, -75.6752 39.290498 0 0, -75.673327 39.291027 0 0, -75.670301999999992 39.290890999999995 0 0, -75.669276 39.291396999999996 0 0, -75.66726899999999 39.290417 0 0, -75.665810999999991 39.290566999999996 0 0, -75.660476 39.291672 0 0, -75.654461 39.291184 0 0, -75.652947 39.291948999999995 0 0, -75.65135 39.291588999999995 0 0, -75.64917899999999 39.293151 0 0, -75.647686 39.29588 0 0, -75.646293 39.29687 0 0, -75.6427 39.299107 0 0, -75.641928 39.300891 0 0, -75.640875 39.301224 0 0, -75.633937 39.301593 0 0, -75.63225 39.301327 0 0, -75.627530999999991 39.304438 0 0, -75.626132 39.30413 0 0, -75.62404699999999 39.306376 0 0, -75.623263999999992 39.308029 0 0, -75.62137899999999 39.308755 0 0, -75.619631 39.310058 0 0, -75.61605999999999 39.309734999999996 0 0, -75.614256 39.308307 0 0, -75.612008 39.308954 0 0, -75.610863 39.309276 0 0, -75.609385 39.308800999999995 0 0, -75.606597999999991 39.30809 0 0, -75.603836 39.311181999999995 0 0, -75.603445999999991 39.309094 0 0, -75.602031 39.30811 0 0, -75.599571 39.308442 0 0, -75.599218 39.309737 0 0, -75.597106 39.309554 0 0, -75.596609 39.309647 0 0, -75.594841 39.309903999999996 0 0, -75.59388899999999 39.308417 0 0, -75.592338 39.309948 0 0, -75.592056 39.311251999999996 0 0, -75.590409 39.311746 0 0, -75.58749499999999 39.311009 0 0, -75.587324999999993 39.309698999999995 0 0, -75.58476499999999 39.308644 0 0, -75.581144999999992 39.311130999999996 0 0, -75.57462 39.313434 0 0, -75.57095799999999 39.318844999999996 0 0, -75.56871599957131 39.323523001317263 0 0, -75.569820001173056 39.326576996779295 0 0, -75.569007 39.328744 0 0, -75.56480599999999 39.331936 0 0, -75.562733999999992 39.332104 0 0, -75.558843 39.333334 0 0, -75.556204999999991 39.335325 0 0, -75.555681007372513 39.33653000312443 0 0, -75.55631799999999 39.337879 0 0, -75.55833399521066 39.339230004872931 0 0, -75.557357995926381 39.341500995668405 0 0, -75.557846 39.342763999999995 0 0, -75.559981999999991 39.343466 0 0, -75.56051899792655 39.345066999353946 0 0, -75.5588 39.346548999999996 0 0, -75.557378 39.349101 0 0, -75.55619 39.348287 0 0, -75.555886 39.346743 0 0, -75.554223 39.345403999999995 0 0, -75.551599 39.345105 0 0, -75.551039996261352 39.345567994418367 0 0, -75.551096 39.349483 0 0, -75.552241999999993 39.350156999999996 0 0, -75.553840999999991 39.349938 0 0, -75.554683007400541 39.351506002990355 0 0, -75.553369 39.35301 0 0, -75.551626 39.35394 0 0, -75.549645 39.353854999999996 0 0, -75.548131 39.352920999999995 0 0, -75.54811 39.351713 0 0, -75.545216 39.350763 0 0, -75.544056 39.351363 0 0, -75.543263 39.353235999999995 0 0, -75.541586 39.354366 0 0, -75.539867 39.353887 0 0, -75.53899 39.351954 0 0, -75.535849 39.350617 0 0, -75.534483994047335 39.351388997582582 0 0, -75.53427799305544 39.353332004157252 0 0, -75.531914 39.354594999999996 0 0, -75.528696 39.353429999999996 0 0, -75.527258995814222 39.354675001898435 0 0, -75.528673 39.356781999999995 0 0, -75.52845300020698 39.358305006725651 0 0, -75.525877 39.359642 0 0, -75.523254 39.359711999999995 0 0, -75.521419999999992 39.358607 0 0, -75.519628 39.360065999999996 0 0, -75.516897 39.360988 0 0, -75.515975 39.364363 0 0, -75.51237208219473 39.365656144406771 0 0, -75.512996 39.366153 0 0, -75.515796999999992 39.378347 0 0, -75.52126299999999 39.381654 0 0, -75.521682 39.387871 0 0, -75.523583 39.391583 0 0, -75.535977 39.409383999999996 0 0, -75.538512 39.416502 0 0, -75.555889999999991 39.430351 0 0, -75.571829994645924 39.438896995945825 0 0, -75.5714550448173 39.4404895611251 0 0, -75.5709849993434 39.442485993390129 0 0, -75.573077 39.445373 0 0, -75.578914 39.44788 0 0, -75.5795930686755 39.4494326149262 0 0, -75.580185 39.450786 0 0, -75.589439 39.460812 0 0, -75.589901 39.462022 0 0, -75.592328999999992 39.467577999999996 0 0, -75.593068 39.477996 0 0, -75.593068000877849 39.47918599762091 0 0, -75.587813999749756 39.49032700194055 0 0, -75.587729 39.495369 0 0, -75.587729 39.4957407450199 0 0, -75.587728998315711 39.4963530034779 0 0, -75.586554401533689 39.497688711813 0 0, -75.581140859129292 39.5038448006784 0 0, -75.576436 39.509195 0 0, -75.574502999999993 39.512516999999995 0 0, -75.5728617838115 39.5160342206675 0 0, -75.572364001474554 39.5171009984447 0 0, -75.570362 39.527223 0 0, -75.569418 39.539124 0 0, -75.569358999999992 39.540589 0 0, -75.565636001056717 39.558509006016514 0 0, -75.564649 39.559922 0 0, -75.5641182880885 39.5606837276848 0 0, -75.563034004787326 39.562239998169261 0 0, -75.570782999999992 39.56728 0 0, -75.571598999999992 39.567727999999995 0 0, -75.586016 39.578448 0 0, -75.586608 39.57888 0 0, -75.58702738490949 39.5798547865464 0 0, -75.58720000000001 39.580256 0 0, -75.587744 39.580672 0 0, -75.591984 39.583248 0 0, -75.592224 39.583568 0 0, -75.597173322751 39.5877820945208 0 0, -75.598747 39.589121999999996 0 0, -75.600785536192 39.5890537256847 0 0, -75.603584 39.58896 0 0, -75.60464 39.58992 0 0, -75.611873 39.597408 0 0, -75.611905 39.597567999999995 0 0, -75.613792996204168 39.606191997215134 0 0, -75.613473001357889 39.606832002184781 0 0, -75.613476995906737 39.606860998404279 0 0, -75.6134736873228 39.606868426370596 0 0, -75.613233 39.607408 0 0, -75.611343991850447 39.610182999893205 0 0, -75.61366500749152 39.612559994452383 0 0, -75.613344994092387 39.613056004804442 0 0, -75.614273 39.61464 0 0, -75.614929004901924 39.615951994615266 0 0, -75.614065 39.61832 0 0, -75.613377 39.620287999999995 0 0, -75.613153 39.62096 0 0, -75.611969 39.621967999999995 0 0, -75.6004949488643 39.6353936845212 0 0, -75.5917467759147 39.6456298433782 0 0, -75.587147 39.651012 0 0, -75.5830124717269 39.6522635452232 0 0, -75.569037 39.656493999999995 0 0, -75.562246 39.656712 0 0, -75.5613567608041 39.6577726838033 0 0, -75.5568448905546 39.6631544390364 0 0, -75.5565726400012 39.663479179279292 0 0, -75.5537715895688 39.666820269999704 0 0, -75.5508077933438 39.6703554837718 0 0, -75.550803870881907 39.670360162481195 0 0, -75.5507962726737 39.670369225617904 0 0, -75.5349831512427 39.689231104242 0 0, -75.5319118207207 39.692894584837696 0 0, -75.52553303088601 39.7005032002623 0 0, -75.512682348589891 39.7158314838503 0 0, -75.509113 39.720089 0 0, -75.4964817737103 39.743315194477894 0 0, -75.490242999999992 39.754787 0 0, -75.4897514259047 39.755503457769095 0 0, -75.4719915820485 39.7813880156216 0 0, -75.4697455653652 39.7846615325088 0 0, -75.4690045587194 39.7857415323977 0 0, -75.468502 39.786474 0 0, -75.446497431828192 39.7963039695592 0 0, -75.4234159950776 39.806614995941658 0 0, -75.428038 39.809211999999995 0 0, -75.4339085190138 39.8117473186932 0 0, -75.4370683549011 39.8131119665163 0 0, -75.437307916589788 39.8132154267429 0 0, -75.449874383721593 39.8186425446778 0 0, -75.45374 39.820312 0 0, -75.454997184031086 39.820770300605005 0 0, -75.4598175446831 39.8225275407136 0 0, -75.4599264483802 39.822567241051004 0 0, -75.463341 39.823812 0 0, -75.463456872955192 39.8238463068355 0 0, -75.472547501842 39.826537795193694 0 0, -75.472677629082384 39.826576322336 0 0, -75.4754890471753 39.8274087068895 0 0, -75.481242 39.829111999999995 0 0, -75.481244921815687 39.8291126972119 0 0, -75.4930772205643 39.831936153535004 0 0, -75.498843 39.833312 0 0, -75.4989861331738 39.833333899718795 0 0, -75.518444 39.836310999999995 0 0, -75.5188176150883 39.836344961758094 0 0, -75.539346 39.838211 0 0, -75.5400582754622 39.838229220042 0 0, -75.5408864120625 39.838250403817796 0 0, -75.5513215785919 39.8385173359007 0 0, -75.570464 39.839006999999995 0 0, -75.5793939947411 39.8385493199286 0 0, -75.579849 39.838526 0 0, -75.5798996512343 39.8385220738603 0 0, -75.5930817368867 39.8375002881084 0 0, -75.593666 39.837455 0 0, -75.59484583814249 39.8372862097586 0 0, -75.595756 39.837156 0 0, -75.617251 39.833999 0 0, -75.634706 39.830163999999996 0 0, -75.634812797602493 39.8301357641688 0 0, -75.641517999999991 39.828362999999996 0 0, -75.662821999999991 39.821149999999996 0 0, -75.667142678086009 39.819267244337 0 0, -75.680794056787789 39.813318592285796 0 0, -75.6815568950873 39.812986181932693 0 0, -75.685991 39.811054 0 0, -75.701208 39.802606 0 0, -75.705787295543 39.7995238880071 0 0, -75.716968999999992 39.791998 0 0, -75.7193345673559 39.7901506045411 0 0, -75.7233991238184 39.7869763794942 0 0, -75.724866264384787 39.785830612575595 0 0, -75.727049 39.784126 0 0, -75.736488999999992 39.775759 0 0, -75.7393235590979 39.77292479948 0 0, -75.7393766862116 39.772871679087004 0 0, -75.744394 39.767855 0 0, -75.751436564661 39.759552050150596 0 0, -75.75306599999999 39.757630999999996 0 0, -75.7544659060403 39.7556311342282 0 0, -75.7592137517567 39.748848497490393 0 0, -75.760101611338087 39.7475801266599 0 0, -75.760346 39.747231 0 0, -75.7658791814134 39.7381059004001 0 0, -75.766058 39.737811 0 0, -75.773786 39.7222 0 0, -75.788595997316847 39.722198993930377 0 0)))'
, ' 0 0', ''
)
) as polygon
), b as ( select to_geography('POINT(-75.677 39.708)') point)
select ST_ASWKT(point)
, ST_ASWKT(polygon)
, st_intersects(polygon, point) intersects
from a, b
(previously the error was 100256: Error parsing Geo input)
--
Bonus, charting with st_simplify(p, 100) just to see how it changes:

Design a specific algorithm for a nxn array implemented on O(nlogn)

The problem:
Suppose that each row of an n×n array A consists of 1’s and 0’s such that, in any row of A, all the 1’s come before any 0’s in that row. Assuming A is already in memory, describe a method running in O(nlogn) time (not O(n2) time!) for counting the number of 1’s in A.
My experience: I have done it for O(n) but I dont know how can I achieve it with O(nlogN)
I would appreciate any help !
Consider that each individual row consists of all 1s followed by all 0s:
1111111000
You can use a binary search to find the transition point (the last 1 in the row). The way this works is to set low and high to the ends and check the middle.
If you are at the transition point, you're done. Otherwise, if you're in the 1s, set low to one after the midpoint. Otherwise, you're in the 0s, so set high to one before the midpoint.
That would go something like (pseudo-code, with some optimisations):
def countOnes(row):
# Special cases first, , empty, all 0s, or all 1s.
if row.length == 0: return 0
if row[0] == "0": return 0
if row[row.length - 1] == 1: return row.length
# At this point, there must be at least one of each value,
# so length >= 2. That means you're guaranteed to find a
# transition point.
lo = 0
hi = row.length - 1
while true:
mid = (lo + hi) / 2
if row[mid] == 1 and row[mid+1] == 0:
return mid + 1
if row[mid] == 1:
lo = mid + 1
else:
hi = mid - 1
Since a binary search for a single row is O(logN) and you need to do that for N rows, the resultant algorithm is O(NlogN).
For a more concrete example, see the following complete Python program, which generates a mostly random matrix then uses the O(N) method and the O(logN) method (the former as confirmation) of counting the ones in each row:
import random
def slow_count(items):
count = 0
for item in items:
if item == 0:
break
count += 1
return count
def fast_count(items):
# Special cases first, no 1s or all 1s.
if len(items) == 0: return 0
if items[0] == 0: return 0
if items[len(items) - 1] == 1: return len(items)
# At this point, there must be at least one of each value,
# so length >= 2. That means you're guaranteed to find a
# transition point.
lo = 0
hi = len(items) - 1
while True:
mid = (lo + hi) // 2
if items[mid] == 1 and items[mid+1] == 0:
return mid + 1
if items[mid] == 1:
lo = mid + 1
else:
hi = mid - 1
# Ensure test data has rows with all zeros and all ones.
N = 20
matrix = [[1] * N, [0] * N]
# Populate other rows randomly.
random.seed()
for _ in range(N - 2):
numOnes = random.randint(0, N)
matrix.append([1] * numOnes + [0] * (N - numOnes))
# Print rows and counts using slow-proven and fast method.
for row in matrix:
print(row, slow_count(row), fast_count(row))
The fast_count function is the equivalent of what I've provided in this answer.
A sample run is:
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] 20 20
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 0 0
[1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 5 5
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0] 15 15
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 10 10
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 1 1
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0] 11 11
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0] 12 12
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0] 11 11
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 1 1
[1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 6 6
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0] 16 16
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0] 14 14
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0] 11 11
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 9 9
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0] 13 13
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 1 1
[1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 4 4
[1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 6 6
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0] 19 19

python: vectorized cumulative counting

I have a numpy array and would like to count the number of occurences for each value, however, in a cumulative way
in = [0, 1, 0, 1, 2, 3, 0, 0, 2, 1, 1, 3, 3, 0, ...]
out = [0, 0, 1, 1, 0, 0, 2, 3, 1, 2, 3, 1, 2, 4, ...]
I'm wondering if it is best to create a (sparse) matrix with ones at col = i and row = in[i]
1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0
0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0
0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0
Then we could compute the cumsums along the rows and extract the numbers from the locations where the cumsums increment.
However, if we cumsum a sparse matrix, doesn't become dense? Is there an efficient way of doing it?
Here's one vectorized approach using sorting -
def cumcount(a):
# Store length of array
n = len(a)
# Get sorted indices (use later on too) and store the sorted array
sidx = a.argsort()
b = a[sidx]
# Mask of shifts/groups
m = b[1:] != b[:-1]
# Get indices of those shifts
idx = np.flatnonzero(m)
# ID array that will store the cumulative nature at the very end
id_arr = np.ones(n,dtype=int)
id_arr[idx[1:]+1] = -np.diff(idx)+1
id_arr[idx[0]+1] = -idx[0]
id_arr[0] = 0
c = id_arr.cumsum()
# Finally re-arrange those cumulative values back to original order
out = np.empty(n, dtype=int)
out[sidx] = c
return out
Sample run -
In [66]: a
Out[66]: array([0, 1, 0, 1, 2, 3, 0, 0, 2, 1, 1, 3, 3, 0])
In [67]: cumcount(a)
Out[67]: array([0, 0, 1, 1, 0, 0, 2, 3, 1, 2, 3, 1, 2, 4])

Resources