Splitting a vector into several vectors when there is a change at a certain digit - matlab

Let we have vector in Matlab such as
V =
0.000178395155017
0.000213248167386
0.000453830296775
*0.000954267024225
0.001985203786879
0.002752603423106
0.004236131760631
0.005323800592906
0.004742807895286
0.005770068613768
0.006502644472601
0.007193873735489
0.006799579314313
0.007976014911026
0.007799267839386
0.008907651205854
0.009190413742866
0.008776108424259
0.008578897868764
0.009250162682559
0.008646692914164
0.008053809730819
0.007451047998031
0.006729120942615
0.005979207722644
0.007134289275269
0.005186062958380
0.005902382106719
0.007762189959679
0.008579238141806
0.009485819832702
0.009780236553648
0.010538511758940
0.012865379338644
0.011390556215603
0.010804154725939
0.010290545066940
0.009868360169125
0.009357083973682
0.008724843328595
0.008560695909341
0.008784388594649
0.008739349797176
0.008670604330509
0.008753566233007
0.008490628616888
0.008003101497979
0.008389140601319
0.007282172470936
0.006743970005005
0.006452902620905
0.005895958422668
0.006326020573140
0.005740103704853
0.005142834616900
0.004407465481546
0.003835738957490
0.004510955842245
0.003693705366274
0.002915993815608
0.002889010498571
0.003093844935832
0.004282403818479
0.003970929347537
0.002819774971736
0.003095369398698
0.002920594268954
0.002431265415582
0.001949299679935
0.001496954638334
0.001096892239592
0.001092281820070
0.000790749599802
0.000893827364410
0.001334675844874
0.001196368539888
0.001160349994386
0.001020290171146
0.001112407234178
0.001747377249787
0.002765678114897
0.003478849585666
0.005355127628032
0.004986389269550
0.005947847852449
0.006157789251547
0.006181425650566
0.006194151127049
0.006269927746287
0.006769754494212
0.007712318414790
0.008651586166082
0.008790606282214
0.009736113450469
0.011185160343747
0.012691072774672
0.010504478607987
0.014389683527318
0.014831212205190
0.028483974800043
0.011080767592010
0.011092374038746
0.012487511417703
0.013592769613900
0.023109284793639
0.011965905202027
0.012215762791363
0.013202467569807
0.011692397524803
0.011053425496442
0.010772141978943
0.010246937424879
0.0099733483767118
0.009945578183562
0.010512885752603
0.009531902580105
0.008066298613627
0.007407338182258
0.007934227536212
0.008839106853309
0.007141630457420
0.005890234651479
0.005777682702918
0.005975075824875
0.007650395349746
0.007857392419299
0.005786229253901
0.005200025773704
0.004125852007666
0.004066689824078
0.003581520984740
0.001457233291953
0.001322814038819
0.001253430603776
0.002493349764335
0.001702719375177
0.001556075727362
0.001200788268232
0.000940090552841
0.002078710051336
0.001014764365464
0.000954943700664
0.001276066704394
0.001359265944949
0.001204978086943
0.001593370287575
0.001547390763560
0.000930577956978
0.000916566445246
0.000546925307518
0.013565059409590
0.001282356857566
0.015602976997836
0.001499511920717
0.001586946634746
0.014156830819098
0.000291360243454
0.000429438521866
0.000462155358170
0.000362561191605
0.000657801010925
0.000593844882183
0.000609582362382
0.000310044384822
0.000342618178403
0.000282871074224
0.000314326548349
0.000204414827855
0.000337206866696
0.000532099888764
0.001193470342198
0.001296110099774
0.000872141517716
0.000700042872179
0.000881365833141
0.000373364170668
0.000425611062469
0.001421790419507
0.000852308546181
0.000815064422325
0.000504599352342
0.000700524140014
0.000870732973155
0.000714061606598
0.001035552251181
0.001149954823243
0.001055911091689
0.001199405112898
0.001783769543970
0.002409901173348
0.003498367375372
0.003703495953811
0.006277776686302
0.011652755634052
0.010083799009042
0.040863796019376
0.013614001128299
0.011042432683334
0.015305185452370
0.012711371813255
0.016268067330822
0.017370177368730
0.019331483583318
0.014673338103928
0.017693568446981
0.016892965150834
0.014088831405978
0.017200144763667
0.014169123504048
0.010078177083794
0.014268458659556
0.011690206086017
0.006562739851251
0.016466509298505
0.015914670918691
0.005624098695020
0.017555623339083
0.013692853335518
0.004491863547507
0.015326207984803
0.014740709288865
0.003199262301369
0.013665227028721
0.052745092682123
0.029889776608225
0.008659506247240
0.008589034126194
0.007785146195475
0.007151058968527
0.006980966929187
0.005799220068642
0.005386071463278
0.004896307509138
0.004818382921265
0.004623210359464
0.004383594039787
0.003923666862792
0.003524680860004
0.003014782806900
0.002558267907671
0.002794987126812
0.003018175443714
0.003166769934104
0.004138527203301
0.004310037290568
0.004610838173675
0.004947713276471
0.005083932336596
0.005236700123762
0.004687780786133
0.004564827415894
0.004302650724049
0.004270824001320
0.003937584195932
0.003911947276786
0.003620705359817
0.003151576407325
0.002751470077629
0.002669908472863
0.002232914262935
0.002069299781104
0.001979860796735
0.001601476811607
0.000301142580668
0.000321068102647
0.000390207925071
0.000210753154473
0.000387358708178
0.000241971967384
0.000366525972307
0.000619750196280
0.000412347959310
0.000544608934404
0.001311532428278
0.000736536458620
0.000523889409829
0.000492076015774
0.000297176544828
0.000195452121510
0.000292051729488
0.000759425298714
0.004267105657429
0.000192598592911
0.000235881092079
0.000209623369819
0.000160795350619
0.000118611207140
0.000253937140734
0.000229140021426
0.000132672582131
0.000185947357453
0.000336849580242
0.000161741284909
0.000212296295172
0.000129317114703
0.000078122221464
0.000139606283922
0.000074030291037
0.000273018975900
0.000084307182558
Our aim is to split it into some other vectors.
Th criteria to splitting is if:
1- There is a change in element V(i) and V(i+1)of at least 10e-1.
It store the indices into a new vector W and if:
2- their differences in indices is larger than 30, i.e., W(j) - W(j+1) > 30.
If these both conditions are satisfied then the vector would be split till the index i. otherwise would be left.
For example in the given vector, the output indices should be as:
[4, 75, 185]
where the there would be 4 vectors:
V1 = [1:4]
V2 = [4:75]
v3 = [75:185]
V4 = [185:300]
What would you think about choosing a tolerance or having a criteria?
Any idea is really appreciated.

I guess what you want is:
V = [0.0003,0.0008,0.0019,0.0027,0.0034,0.0103,0.0110,0.0106,0.0113,0.0134,0.0143,0.0148,0.0152,0.0156,0.00166,0.00172,0.0174,0.0195,0.0193,0.000195,0.00021]
digit = 2;
output = accumarray(cumsum(logical([0; diff(floor(10^digit*V(:)))]))+1,V,[],#(x) {x})
gives you an cell array.
output =
[5x1 double]
[9x1 double]
[2x1 double]
[3x1 double]
[2x1 double]
some explainations:
%// get the important digit
digs = floor(10^digit*V(:));
%// see where it changes
d = [0; diff(digs)];
%// convert to 0s and 1s and cumsum them up
subs = cumsum(logical(d))+1;
%// to get the subs for accumarray
output = accumarray(subs,V,[],#(x) {x})

Related

Lsode throwing INTDY-- T (=R1) ILLEGAL and invalid input detected

I have my function:
function [result] = my_func(x,y)
result = y^2*(1-3*x)-3*y;
endfunction
Also, my vector with Ts, my function address and my initial variable x_0
load file_with_ts
# Add my limits as I also want to calculate those
# (all values in file_with_ts are within those limits.)
t_points = [-1, file_with_ts, 2]
myfunc = str2func("my_func")
x_0 = 0.9142
I am trying to execute the following line:
lsode_d1 = lsode(myfunc, x_0, t_points)
And expecting a result, but getting the following error:
INTDY-- T (=R1) ILLEGAL
In above message, R1 = 0.7987082301475D+00
T NOT IN INTERVAL TCUR - HU (= R1) TO TCUR (=R2)
In above, R1 = 0.8091168896311D+00 R2 = 0.8280400838323D+00
LSODE-- TROUBLE FROM INTDY. ITASK = I1, TOUT = R1
In above message, I1 = 1
In above message, R1 = 0.7987082301475D+00
error: lsode: invalid input detected (see printed message)
error: called from
main at line 20 column 10
Also, the variable sizes are:
x_0 -> 1x1
t_points -> 1x153
myfunc -> 1x1
I tried transposing the t_points vector
using #my_func instead of the str2func function
I tried adding multiple variables as the starting point (instead of x_0 I entered [x_0; x_1])
Tried changing my function header from my_func(x, y) to my_func(y, x)
Read the documentation and confirmed that my_func allows x to be a vector and returns a vector (whenever x is a vector).
EDIT: T points is the following 1x153 matrix (with -1 and 2 added to the beggining and the end respectively):
-4.9451e-01
-4.9139e-01
-4.7649e-01
-4.8026e-01
-4.6177e-01
-4.5412e-01
-4.4789e-01
-4.2746e-01
-4.1859e-01
-4.0983e-01
-4.0667e-01
-3.8436e-01
-3.7825e-01
-3.7150e-01
-3.5989e-01
-3.5131e-01
-3.4875e-01
-3.3143e-01
-3.2416e-01
-3.1490e-01
-3.0578e-01
-2.9267e-01
-2.9001e-01
-2.6518e-01
-2.5740e-01
-2.5010e-01
-2.4017e-01
-2.3399e-01
-2.1491e-01
-2.1067e-01
-2.0357e-01
-1.8324e-01
-1.8112e-01
-1.7295e-01
-1.6147e-01
-1.5424e-01
-1.4560e-01
-1.1737e-01
-1.1172e-01
-1.0846e-01
-1.0629e-01
-9.4327e-02
-8.0883e-02
-6.6043e-02
-6.6660e-02
-6.1649e-02
-4.7245e-02
-2.8332e-02
-1.8043e-02
-7.7416e-03
-6.5142e-04
1.0918e-02
1.7619e-02
3.4310e-02
3.3192e-02
5.2275e-02
5.5756e-02
6.8326e-02
8.2764e-02
9.5195e-02
9.4412e-02
1.1630e-01
1.2330e-01
1.2966e-01
1.3902e-01
1.4891e-01
1.5848e-01
1.7012e-01
1.8026e-01
1.9413e-01
2.0763e-01
2.1233e-01
2.1895e-01
2.3313e-01
2.4092e-01
2.4485e-01
2.6475e-01
2.7154e-01
2.8068e-01
2.9258e-01
3.0131e-01
3.0529e-01
3.1919e-01
3.2927e-01
3.3734e-01
3.5841e-01
3.5562e-01
3.6758e-01
3.7644e-01
3.8413e-01
3.9904e-01
4.0863e-01
4.2765e-01
4.2875e-01
4.3468e-01
4.5802e-01
4.6617e-01
4.6885e-01
4.7247e-01
4.8778e-01
4.9922e-01
5.1138e-01
5.1869e-01
5.3222e-01
5.4196e-01
5.4375e-01
5.5526e-01
5.6629e-01
5.7746e-01
5.8840e-01
6.0006e-01
5.9485e-01
6.1771e-01
6.3621e-01
6.3467e-01
6.5467e-01
6.6175e-01
6.6985e-01
6.8091e-01
6.8217e-01
6.9958e-01
7.1802e-01
7.2049e-01
7.3021e-01
7.3633e-01
7.4985e-01
7.6116e-01
7.7213e-01
7.7814e-01
7.8882e-01
8.1012e-01
7.9871e-01
8.3115e-01
8.3169e-01
8.4500e-01
8.4168e-01
8.5705e-01
8.6861e-01
8.8211e-01
8.8165e-01
9.0236e-01
9.0394e-01
9.2033e-01
9.3326e-01
9.4164e-01
9.5541e-01
9.6503e-01
9.6675e-01
9.8129e-01
9.8528e-01
9.9339e-01
Credits to Lutz Lehmann and PierU.
The problem lied in the array t_points not being a monotonous array. Adding a sort(t_points) before doing any calculations fixed the error.

Error using integral (line 85): A and B must be floating-point scalars

I have the following code that I wish to estimate the parameters of a custom distribution using MATLAB's function mle(). For more details on the distribution.
The main code is:
x = [0 0 0 0 0.000649967501624919 0.00569971501424929 0.0251487425628719 0.0693465326733663 0.155342232888356 0.284835758212089 0.458277086145693 0.658567071646418 0.908404579771011 1.17284135793210 1.43977801109945 1.71951402429879 1.98925053747313 2.27553622318884 2.57147142642868 2.80390980450977 3.03829808509575 3.26583670816459 3.45642717864107 3.65106744662767 3.81950902454877 3.98275086245688 4.11259437028149 4.24683765811709 4.35043247837608 4.43832808359582 4.58427078646068 4.62286885655717 4.68361581920904 4.75686215689216 4.80245987700615 4.84005799710015 4.86280685965702 4.91675416229189 4.92725363731813 4.90890455477226 4.96570171491425 4.92315384230789 4.95355232238388 4.92790360481976 4.93135343232838 4.90310484475776 4.90885455727214 4.86765661716914 4.85490725463727 4.81940902954852 4.81450927453627 4.78621068946553 4.74206289685516 4.71791410429479 4.69961501924904 4.65706714664267 4.63611819409030 4.60176991150443 4.57512124393780 4.53507324633768 4.48252587370631 4.47062646867657 4.43127843607820 4.39963001849908 4.37598120093995 4.29548522573871 4.31033448327584 4.21708914554272 4.21913904304785 4.18669066546673 4.16719164041798 4.09774511274436 4.07989600519974 4.02869856507175 3.98485075746213 3.95785210739463 3.93945302734863 3.90240487975601 3.87025648717564 3.81185940702965 3.78461076946153 3.74091295435228 3.71666416679166 3.67276636168192 3.65846707664617 3.61361931903405 3.58712064396780 3.55452227388631 3.53082345882706 3.49197540122994 3.48582570871456 3.46512674366282 3.41227938603070 3.36278186090695 3.35528223588821 3.31238438078096 3.27213639318034 3.23863806809660 3.24173791310434 3.19339033048348 3.20118994050298 3.16489175541223 3.10739463026849 3.09484525773711 3.08094595270237 3.02129893505325 3.02309884505775 2.99375031248438 2.95765211739413 2.93230338483076 2.89560521973901 2.87805609719514 2.85440727963602 2.82285885705715 2.80175991200440 2.79091045447728 2.73901304934753 2.72701364931753 2.73441327933603 2.71646417679116 2.68236588170592 2.65551722413879 2.63356832158392 2.60361981900905 2.58147092645368 2.57697115144243 2.54287285635718 2.53502324883756 2.47702614869257 2.50387480625969 2.46487675616219 2.45722713864307 2.42707864606770 2.41762911854407 2.39823008849558 2.38708064596770 2.34058297085146 2.35613219339033 2.32123393830309 2.30503474826259 2.27613619319034 2.27248637568122 2.25113744312784 2.24908754562272 2.22703864806760 2.20583970801460 2.17244137793110 2.15709214539273 2.16469176541173 2.12139393030348 2.12809359532023 2.11389430528474 2.09774511274436 2.07629618519074 2.07459627018649 2.05394730263487 2.04724763761812 2.01684915754212 2.01684915754212 2.00409979501025 1.98955052247388 1.96540172991350 1.95890205489726 1.93035348232588 1.92295385230738 1.90605469726514 1.89785510724464 1.87070646467677 1.88000599970002 1.86295685215739 1.84420778961052 1.82510874456277 1.80480975951202 1.80785960701965 1.80870956452177 1.77581120943953 1.76771161441928 1.77131143442828 1.76636168191590 1.75081245937703 1.73156342182891 1.69876506174691 1.70836458177091 1.70376481175941 1.67196640167992 1.68101594920254 1.66586670666467 1.66061696915154 1.64296785160742 1.63291835408230 1.62506874656267 1.62516874156292 1.60556972151392 1.59007049647518 1.59187040647968 1.57947102644868 1.57577121143943 1.54527273636318 1.57237138143093 1.54637268136593 1.54802259887006 1.50492475376231 1.52077396130193 1.50417479126044 1.50162491875406 1.50062496875156 1.48957552122394 1.47997600119994 1.47027648617569 1.44452777361132 1.45407729613519 1.44272786360682 1.43247837608120 1.41657917104145 1.40787960601970 1.39323033848308 1.40282985850707 1.39403029848508 1.38233088345583 1.37888105594720 1.37943102844858 1.36183190840458 1.34808259587021 1.34503274836258 1.33703314834258 1.33308334583271 1.32253387330633 1.32698365081746 1.29963501824909 1.30758462076896 1.29103544822759 1.29473526323684 1.27413629318534 1.26858657067147 1.27888605569722 1.26063696815159 1.27863606819659 1.25168741562922 1.23913804309785 1.24788760561972 1.22308884555772 1.24198790060497 1.22133893305335 1.20678966051697 1.20098995050247 1.20343982800860 1.18779061046948 1.19024048797560 1.17194140292985 1.17369131543423 1.16869156542173 1.15814209289536 1.15429228538573 1.15904204789761 1.12774361281936 1.15344232788361 1.13744312784361 1.12909354532273 1.12479376031198 1.11099445027749 1.11469426528674 1.11064446777661 1.10464476776161 1.10309484525774 1.10689465526724 1.07654617269137 1.07884605769712 1.07359632018399 1.06864656767162 1.07544622768862 1.06689665516724 1.04884755762212 1.06164691765412 1.04979751012449 1.04529773511324 1.02839858007100 1.03634818259087 1.01709914504275 1.02089895505225 1.01024948752562 1.01549922503875 1.01319934003300 1.01404929753512 1.00839958002100 0.995400229988501 0.989850507474626 0.978801059947003 0.977551122443878 0.980450977451127 0.975451227438628 0.969201539923004 0.964151792410380 0.964601769911504 0.958802059897005 0.955702214889256 0.948602569871506 0.960751962401880 0.941352932353382 0.928653567321634 0.949002549872506 0.937053147342633 0.913854307284636 0.916204189790510 0.915454227288636 0.902604869756512 0.909454527273636 0.895505224738763 0.898355082245888 0.894455277236138 0.902454877256137 0.883705814709265 0.888405579721014 0.876356182190891 0.881555922203890 0.878156092195390 0.868456577171141 0.870406479676016 0.863906804659767 0.862456877156142 0.858757062146893 0.851307434628269 0.851107444627769 0.833908304584771 0.843507824608770 0.831708414579271 0.836858157092145 0.829058547072646 0.828508574571272 0.822908854557272 0.820508974551273 0.815559222038898 0.819709014549273 0.809609519524024 0.813409329533523 0.800759962001900 0.806609669516524 0.806959652017399 0.792260386980651 0.787660616969152 0.783810809459527 0.794960251987401 0.771061446927654 0.788910554472276 0.789510524473776 0.763061846907655 0.776761161941903 0.767561621918904 0.773611319434028 0.750262486875656 0.765811709414529 0.765911704414779 0.748012599370032 0.741612919354032 0.757312134393280 0.752612369381531 0.741362931853407 0.742212889355532 0.741912904354782 0.743162841857907 0.732963351832408 0.732813359332033 0.733363331833408 0.721913904304785 0.716664166791661 0.726713664316784 0.709764511774411 0.700064996750163 0.710764461776911 0.717664116794160 0.707314634268287 0.707114644267787 0.705614719264037 0.709164541772911 0.696665166741663 0.680765961701915 0.686715664216789 0.694465276736163 0.683015849207540 0.681715914204290 0.694465276736163 0.688615569221539 0.680665966701665 0.672316384180791 0.672866356682166 0.656517174141293 0.665316734163292 0.671566421678916 0.666266686665667 0.652917354132293 0.663366831658417 0.651917404129794 0.663816809159542 0.661366931653417 0.647017649117544 0.655167241637918 0.647867606619669 0.636918154092295 0.645467726613669 0.633118344082796 0.640217989100545 0.634668266586671 0.618669066546673 0.635068246587671 0.632568371581421 0.623118844057797 0.623868806559672 0.623718814059297 0.621368931553422 0.623768811559422 0.608419579021049 0.616019199040048 0.609869506524674 0.606569671516424 0.614019299035048 0.610269486525674 0.596520173991300 0.595570221488926 0.593270336483176 0.596670166491675 0.598470076496175 0.597770111494425 0.593720313984301 0.592770361481926 0.585420728963552 0.580870956452177 0.584120793960302 0.580270986450677 0.577971101444928 0.579021048947553 0.572821358932053 0.585970701464927 0.572921353932303 0.567071646417679 0.569971501424929 0.571271436428179 0.568421578921054 0.567421628918554 0.569521523923804 0.563721813909305 0.558772061396930 0.562171891405430 0.557872106394680 0.549072546372681 0.558722063896805 0.536973151342433 0.561021948902555 0.544172791360432 0.552122393880306 0.553072346382681 0.546222688865557 0.551472426378681 0.540772961351932 0.541122943852807 0.542772861356932 0.530323483825809 0.526023698815059 0.529273536323184 0.524573771311435 0.525923703814809 0.524923753812309 0.516474176291185 0.527273636318184 0.527723613819309 0.518424078796060 0.517874106294685 0.516074196290186 0.517924103794810 0.523173841307935 0.514474276286186 0.513174341282936 0.498875056247188 0.518024098795060 0.507924603769812 0.505524723763812 0.507174641267937 0.502874856257187 0.502624868756562 0.500624968751562 0.510824458777061 0.490925453727314 0.492675366231688 0.489925503724814 0.478126093695315 0.485775711214439 0.491775411229439 0.489925503724814 0.491325433728314 0.487225638718064 0.485725713714314 0.485675716214189 0.477676116194190 0.483875806209690 0.478026098695065 0.470176491175441 0.471926403679816 0.483625818709065 0.469376531173441 0.474026298685066 0.467826608669567 0.462426878656067];
Censored = ones(1,size(x,2));%
custpdf = #eval_custpdf;
custcdf = #eval_custcdf;
phat = mle(x,'pdf', custpdf,'cdf', custcdf,'start',[1 0.1 0.3 0.1 0.01 -0.3],...
'lowerbound',[0 0 0 0 0 -inf],'upperbound',[inf inf inf inf inf inf],'Censoring',Censored);
% Cheking how close the estimated PDF and CDF match with those from the data x
t = 0.001:0.001:0.5;
figure();
plot(t,x);hold on
plot(t,custpdf(t, phat(1), phat(2), phat(3), phat(4), phat(5), phat(6)))
figure();
plot(t,cumsum(x)./sum(x));hold on
plot(t,custcdf(t, phat(1), phat(2), phat(3), phat(4), phat(5), phat(6)))
The functions are:
function out = eval_custpdf(x,myalpha,mybeta,mytheta,a,b,c)
first_integral = integral(#(x) eval_K(x,a,b,c),0,1).^-1;
theta_t_ratio = (mytheta./x);
incomplete_gamma = igamma(myalpha,theta_t_ratio.^mybeta);
n_gamma = gamma(myalpha);
exponent_term = exp(-theta_t_ratio.^mybeta-(c.*(incomplete_gamma./n_gamma)));
numerator = first_integral.* mybeta.*incomplete_gamma.^(a-1).*...
theta_t_ratio.^(myalpha*mybeta+1).*exponent_term;
denominator = mytheta.* n_gamma.^(a+b-1).* (n_gamma-incomplete_gamma.^mybeta).^(1-b);
out = numerator./denominator;
end
function out = eval_custcdf(x,myalpha,mybeta,mytheta,a,b,c)
first_integral = integral(#(x) eval_K(x,a,b,c),0,1).^-1;
theta_t_ratio = (mytheta./x);
incomplete_gamma = igamma(myalpha,theta_t_ratio.^mybeta);
n_gamma = gamma(myalpha);
second_integral = integral(#(x) eval_K(x,a,b,c),0, incomplete_gamma.^mybeta./n_gamma);
% |<----- PROBLEMATIC LINE ----->|
out = first_integral*second_integral;
end
function out = eval_K(x,a,b,c)
out = x.^(a-1).*(1-x).^(b-1).*exp(-c.*x);
end
The integral that is causing the problem is the second intergral in the function eval_custcdf() as its upper limit is an array (denoted by PROBLEMATIC LINE).
Is there a way to take a single value from the array x such that the upper limit remains a scalar? And then calculate the cdf such that the output of the cdf is an array? Using a forloop, maybe? But I cannot seem to figure how to implement that?
How can I work around this problem?
Any help would be appreciated.
Thanks in advance.
eval_custcdf is a function expected to return 1D array of length n for
a given n data input.
I use a for loop to compute the output for a given input, then
return the whole array as output of eval_custcdf
I passed the input array elements one at a time
This is how eval_custcdf may look like
function out = eval_custcdf(x,myalpha,mybeta,mytheta,a,b,c)
out = zeros(size(x));
for i = 1: length(x)
first_integral = integral(#(w) eval_K(w,a,b,c),0,1).^-1;
theta_t_ratio = (mytheta./x(i));
incomplete_gamma = igamma(myalpha,theta_t_ratio.^mybeta);
n_gamma = gamma(myalpha);
second_integral = integral(#(w) eval_K(w,a,b,c),0, incomplete_gamma.^mybeta./n_gamma);
out(i) = first_integral*second_integral;
end
end

Using interp2 - The grid must be created from grid vectors which are strictly monotonically increasing

I am using MATLAB_R2016b.
I have data of this format
Temperature = [310:10:800];
Pressure = [0.1 0 1.0 10.0 100.0 1000.0];
Cv = ...
[ 73.6400 25.3290 73.5920 73.1260 69.4500 61.8600
72.8060 25.3810 72.7640 72.3450 68.9780 61.7040
71.9230 25.4380 71.8850 71.5070 68.4230 61.3140
71.0060 25.4990 70.9710 70.6290 67.8040 60.8160
70.0680 25.5640 70.0360 69.7270 67.1400 60.2840
69.1220 25.6340 69.0940 68.8140 66.4460 59.7550
68.1800 25.7070 68.1540 67.9000 65.7350 59.2500
27.6640 25.7840 67.2240 66.9940 65.0150 58.7780
27.3630 25.8640 66.3120 66.1040 64.2950 58.3390
27.1700 25.9480 65.4220 65.2330 63.5820 57.9340
27.0440 26.0340 64.5570 64.3850 62.8790 57.5600
26.9660 26.1230 63.7210 63.5640 62.1900 57.2130
26.9240 26.2150 62.9130 62.7700 61.5170 56.8890
26.9110 26.3090 62.1360 62.0050 60.8620 56.5870
26.9200 26.4050 61.3890 61.2690 60.2250 56.3020
26.9460 26.5030 33.1250 60.5620 59.6080 56.0320
26.9870 26.6030 31.8460 59.8850 59.0090 55.7750
27.0390 26.7050 31.0570 59.2360 58.4290 55.5290
27.1010 26.8080 30.5000 58.6170 57.8680 55.2920
27.1700 26.9120 30.0840 58.0280 57.3240 55.0630
27.2460 27.0170 29.7670 57.4700 56.7980 54.8410
27.3280 27.1240 29.5260 56.9450 56.2900 54.6250
27.4140 27.2320 29.3430 56.4560 55.7970 54.4150
27.5040 27.3410 29.2080 56.0070 55.3210 54.2090
27.5980 27.4500 29.1110 55.6040 54.8600 54.0080
27.6940 27.5610 29.0460 55.2610 54.4150 53.8100
27.7930 27.6720 29.0060 54.9970 53.9840 53.6160
27.8950 27.7840 28.9870 54.8470 53.5670 53.4260
27.9980 27.8970 28.9870 51.7540 53.1650 53.2390
28.1030 28.0110 29.0020 47.2710 52.7760 53.0550
28.2100 28.1250 29.0290 44.3160 52.4010 52.8750
28.3180 28.2400 29.0670 42.1390 52.0390 52.6980
28.4270 28.3550 29.1150 40.4520 51.6910 52.5230
28.5380 28.4710 29.1710 39.1070 51.3570 52.3520
28.6500 28.5880 29.2340 38.0170 51.0350 52.1840
28.7630 28.7060 29.3040 37.1240 50.7260 52.0200
28.8770 28.8240 29.3780 36.3870 50.4300 51.8580
28.9920 28.9420 29.4580 35.7750 50.1460 51.7000
29.1080 29.0610 29.5420 35.2640 49.8730 51.5440
29.2250 29.1810 29.6290 34.8380 49.6100 51.3930
29.3420 29.3010 29.7200 34.4810 49.3570 51.2440
29.4610 29.4220 29.8150 34.1820 49.1120 51.0990
29.5800 29.5440 29.9120 33.9330 48.8720 50.9570
29.6990 29.6660 30.0110 33.7250 48.6360 50.8190
29.8200 29.7880 30.1130 33.5540 48.4000 50.6830
29.9410 29.9110 30.2170 33.4130 48.1630 50.5520
30.0630 30.0340 30.3230 33.3000 47.9210 50.4230
30.1850 30.1580 30.4310 33.2100 47.6720 50.2990
30.3080 30.2820 30.5400 33.1400 47.4140 50.1770
30.4310 30.4070 30.6510 33.0890 47.1430 50.0590];
When I try to query a new [temperature, pressure] pair, for example [0.2, 341] by doing this
interp2(Temperature, Pressure, Cv, 0.2, 341)
I get the following error:
Error using griddedInterpolant
The grid vectors must be strictly monotonically increasing.
Error in interp2>makegriddedinterp (line 229)
F = griddedInterpolant(varargin{:});
Error in interp2 (line 129)
F = makegriddedinterp({X, Y}, V, method,extrap);
What am I doing wrong? And how can I get the desired result?
You need to have the same number of points in Temperature and Pressure as you do in Cv. You can generate these points using meshgrid.
[Temp, Pres] = meshgrid(Temperature, Pressure)
% Temp and Pres are both 6x50 matrices
However, you still have an issue. Temperature and Pressure must be monotonically increasing, as the error message states. This means you can't have a pressure value go down, which it does. You must change the second value in the Pressure array, or for instance you may want to swap columns 1 and 2 of the Pressure and Cv arrays
Pressure = [0.1, 0, 1, 10, 100, 1000]; % original
Pressure = Pressure([2, 1, 3:end]); % swap columns 1 and 2, Pressure = [0,0.1,1,10,...]
Cv = [...]; % All of your data
Cv = Cv(:, [2, 1, 3:end]) % Swap columns 1 and 2
Now you can do your lookup. Note you also had the temperature and pressure values the wrong way around, they must be the same order for inputs 1/2 and inputs 4/5.
[Temp, Pres] = meshgrid(Temperature, Pressure);
out = interp2(Temp, Pres, Cv.', 341, 0.2) % not (..., 0.2, 341) as must be same order
>> out = 30.5468
The only consideration you might want to give is that you're using linear interpolation with interp2, but your pressure data is logarithmic. Check your results are sensible.

Quiver list and labeling vectors of my inputs u,d, and r

How do I label each vector of each input component, say u$_1$..., d$_1$..., on my graph at each vector's respective starting point. I want to see if there is a clustering of certain vectors like u$_30$-u$_50$ are all near each other, for example. Here is my code:
fx = [1.01, 1.0165376460248143, 1.016585505356985,...
1.0166786731186357, 1.0166445649045002, 1.01684528204491,...
1.0168363943981442, 1.0169505828006045, 1.0169903647693619,...
1.0170588800755562, 1.0170214556321182, 1.0171007103163394,...
1.0170611565299144, 1.0171737504115423, 1.0171325089936156,...
1.0173884633568437, 1.0173821295549914, 1.017540453473392,...
1.0176091468862674, 1.0177647297690604, 1.017711866139699,...
1.0177536635811828, 1.0178254876275734, 1.0173994306983212,...
1.0200331803664529, 1.0232411092365432, 1.0232773133875106,...
1.023383936276914, 1.0233275057530007, 1.023510835824228,...
1.0234461433923472, 1.023507118352957, 1.0237210297124908,...
1.0236390252916325, 1.0237007559499636, 1.0239084387662698,...
1.0238131746118633, 1.024266374303865, 1.024212732428539,...
1.02440393427416, 1.0245390401237269, 1.0252178000353167,...
1.0252021019242434, 1.0275875709904758, 1.0275871039342042];
fy = [0.99, 0.99, 0.9899194500620327, 0.9897134368447225,...
0.9899339650105077, 0.9895259027418399, 0.9898115223446341,...
0.9896762515189842, 0.9896129792784014, 0.9894766621994305,...
0.9896189382715079, 0.9894614440540032, 0.9896292673356496,...
0.9894095770062209, 0.989655005387203, 0.9892019096930893,...
0.9894189058876284, 0.9892732425545386, 0.9891916768216495,...
0.9889512723219249, 0.9892071461243063, 0.9891515372181835,...
0.9890346980816267, 0.9901802532401042, 0.9892771992437573,...
0.9881487558751526, 0.9880037699743045, 0.9875669935217211,...
0.9878502051001951, 0.9872010568874899, 0.9875329139453003,...
0.9873775054641964, 0.9868251990627905, 0.9871082986923524,...
0.9869819983991632, 0.9865548473263468, 0.9867867860622922,...
0.9859765136441385, 0.9861731333993694, 0.9859212446482857,...
0.9857475603282838, 0.9848759880952044, 0.9850648602644492,...
0.9821891156159342, 0.9822254068452594];
fz =[0.01, -0.0014683388934621459, -0.0028093690242636917,...
-0.006255424514110392, -0.002405171080788649, -0.009167776104980133,...
0.0003750210183572269, -0.001823375333180016, -0.002906415137850454,...
-0.005227263048381278, -0.0028662319950483552, -0.0055329993182467365,...
-0.0027458980004112996, -0.00644276568444028, -0.00226410433801184,...
-0.009832266892691467, 0.0012478354917326469, -0.001163969711179093,...
-0.0026270200357900887, -0.006946260715800828, -0.00188587841967576,...
-0.002880843788516535, -0.0049636661241180685, 0.015586949435911355,...
0.010368914010693711, -0.0010649331940053245, -0.002328942248654949,...
-0.006634620630021168, -0.0020052485893380344, -0.008543368794125199,...
-0.00044976575279103564, -0.0019790036016751333, -0.008330963679008077,...
-0.0006481277669472506, -0.0020539789179887767, -0.0075781311330381336,...
-0.001294365366809558, -0.011629381859506432, 0.003447063734076782,...
0.0011256038145771368, -0.0008637305140054806, -0.012865086502170518,...
0.005283762238371167, -0.016926299226379265, 0.011993515880473204];
x = [1.01, 1.0165376460248143, 1.016585505356985, 1.0166786731186357,...
1.0166445649045002, 1.01684528204491, 1.0168363943981442,...
1.0169505828006045, 1.0169903647693619, 1.0170588800755562,...
1.0170214556321182, 1.0171007103163394, 1.0170611565299144,...
1.0171737504115423, 1.0171325089936156, 1.0173884633568437,...
1.0173821295549914, 1.017540453473392, 1.0176091468862674,...
1.0177647297690604, 1.017711866139699, 1.0177536635811828,...
1.0178254876275734, 1.0173994306983212, 1.0200331803664529,...
1.0232411092365432, 1.0232773133875106, 1.023383936276914,...
1.0233275057530007, 1.023510835824228, 1.0234461433923472,...
1.023507118352957, 1.0237210297124908, 1.0236390252916325,...
1.0237007559499636, 1.0239084387662698, 1.0238131746118633,...
1.024266374303865, 1.024212732428539, 1.02440393427416,...
1.0245390401237269, 1.0252178000353167, 1.0252021019242434,...
1.0275875709904758, 1.0275871039342042];
y = [0.99, 0.99, 0.9899194500620327, 0.9897134368447225,...
0.9899339650105077, 0.9895259027418399, 0.9898115223446341,...
0.9896762515189842, 0.9896129792784014, 0.9894766621994305,...
0.9896189382715079, 0.9894614440540032, 0.9896292673356496,...
0.9894095770062209, 0.989655005387203, 0.9892019096930893,...
0.9894189058876284, 0.9892732425545386, 0.9891916768216495,...
0.9889512723219249, 0.9892071461243063, 0.9891515372181835,...
0.9890346980816267, 0.9901802532401042, 0.9892771992437573,...
0.9881487558751526, 0.9880037699743045, 0.9875669935217211,...
0.9878502051001951, 0.9872010568874899, 0.9875329139453003,...
0.9873775054641964, 0.9868251990627905, 0.9871082986923524,...
0.9869819983991632, 0.9865548473263468, 0.9867867860622922,...
0.9859765136441385, 0.9861731333993694, 0.9859212446482857,...
0.9857475603282838, 0.9848759880952044, 0.9850648602644492,...
0.9821891156159342, 0.9822254068452594];
z =[0.01, -0.0014683388934621459, -0.0028093690242636917,...
-0.006255424514110392, -0.002405171080788649, -0.009167776104980133,...
0.0003750210183572269, -0.001823375333180016, -0.002906415137850454,...
-0.005227263048381278, -0.0028662319950483552, -0.0055329993182467365,...
-0.0027458980004112996, -0.00644276568444028, -0.00226410433801184,...
-0.009832266892691467, 0.0012478354917326469, -0.001163969711179093,...
-0.0026270200357900887, -0.006946260715800828, -0.00188587841967576,...
-0.002880843788516535, -0.0049636661241180685, 0.015586949435911355,...
0.010368914010693711, -0.0010649331940053245, -0.002328942248654949,...
-0.006634620630021168, -0.0020052485893380344, -0.008543368794125199,...
-0.00044976575279103564, -0.0019790036016751333, -0.008330963679008077,...
-0.0006481277669472506, -0.0020539789179887767, -0.0075781311330381336,...
-0.001294365366809558, -0.011629381859506432, 0.003447063734076782,...
0.0011256038145771368, -0.0008637305140054806, -0.012865086502170518,...
0.005283762238371167, -0.016926299226379265, 0.011993515880473204];
figure
q = quiver3(fx,fy,fz,x,y,z)
The problem is that I want to label the starting point of each vector:
You can use text:
q = quiver3(fx,fy,fz,x,y,z)
text(fx,fy,fz,num2str((1:numel(fx)).'))
The first 3 inputs are the coordinates of the label and the next input is a list (column character array or a cell-array) of the labels. I don't understand how your labeling is working (i.e. what is u$_1$ or d$_1$), so I just numbered the vectors from 1 to 45.

find corresponding peaks in matlab with 95% confidence interval

Suppose that we have following array:
0.196238259763928
0.0886250228175519
0.417543614272817
0.182403230538167
0.136500793051860
0.389922187581014
0.0344012946153299
0.381603315802419
0.0997542838649466
0.274807632628596
0.601652859233616
0.209431489000677
0.396925294300794
0.0351587496999554
0.177321874549738
0.369200511917405
0.287108838007101
0.477076452316346
0.127558716868438
0.792431584110476
0.0459982776925879
0.612598437936600
0.228340227044324
0.190267907472804
0.564751537228850
0.00269368929400299
0.940538666131177
0.101588565140294
0.426175626669060
0.600215481734847
0.127859067121782
0.985881201195063
0.0945679498528667
0.950077461673118
0.415212985598547
0.467423473845033
1.24336273213410
0.0848695928658021
1.84522775800633
0.289288949281834
1.38792131632743
1.73186592736729
0.554254947026916
3.46075557122590
0.0872957577705428
4.93259798197976
2.03544238985229
3.71059303259615
8.47095716918618
0.422940369071662
25.2287636895831
4.14535369056670
63.7312173032838
152.080907190007
1422.19492782494
832.134744027851
0.0220089962114756
60.8238733887811
7.71053463387430
10.4151913932115
11.3141744831953
0.988978595613829
8.65598040591953
0.219820300144944
3.92785491164888
2.28370963778411
1.60232807621444
2.51086405960291
0.0181622519984990
2.27469230188760
0.487809730727909
0.961063613990814
1.90435488292485
0.515640996120482
1.25933693517960
0.0953200831348589
1.52851575480462
0.582109930768162
0.933543409438383
0.717947488528521
0.0445235241119612
1.21157308704582
0.0942421028083462
0.536069075206508
0.821400666720535
0.308956823975938
1.28706199713640
0.0339217632187507
1.19575886464231
0.0853733920496230
0.736744959694641
0.635218502184121
0.262305581223588
0.986899895695809
0.0398800891449550
0.758792061180657
0.134279188964854
0.442531129290843
0.542782326712391
0.377221037448628
0.704787750202814
0.224180325609783
0.998785634315287
0.408055416702400
0.329684702125840
0.522384453408780
0.154542718256493
0.602294251721841
0.240357912028348
0.359040779285709
0.525224294805813
0.427539247203335
0.624034405807298
0.298184846094056
0.498659616687732
0.0962076792277457
0.430092706132805
0.656212420735658
0.278310520474744
0.866037361133916
0.184971060800812
0.481149730712771
0.624405636807668
0.382388147099945
0.435350646037440
0.216499523971397
1.22960953802959
0.330841706900755
0.891793067878849
0.628241046456751
0.278687691121678
1.06358076764171
0.365652714373067
1.34921178081181
0.652888708375276
0.861138633227739
1.02878577330537
0.591174450919664
1.93594290806582
0.497631035062465
1.14486512201656
0.978067581547298
0.948931658572253
2.01004088022982
0.917415940349743
2.24124811810385
1.42691656876436
2.15636037453584
1.92812357585099
1.12786835077183
4.81721425534142
1.70055431306602
4.87939454466131
3.90293284926105
5.16542230018432
10.5783535493504
1.74023535081791
27.0572221453758
7.78813114379733
69.2528169436690
167.769806437531
1490.03057130613
869.247150795648
3.27543244752518
62.3527480644562
9.74192115073051
13.6074209231800
10.5686495478844
7.70239986387120
9.62850426896699
9.85304975304259
7.09026325332085
12.8782040428502
16.3163128995995
7.00070066635845
74.1532966917877
4.80506505312457
1042.52337489620
1510.37374385290
118.514435606795
80.7915675273571
2.96352221859211
27.7825124315786
1.55102367292252
8.66382951478539
5.02910503820560
1.25219344189599
7.72195587189507
0.356973215117373
6.06702456628919
1.01953617014621
2.76489896186652
3.35353608882459
0.793376336025486
4.90341095941571
0.00742857354167949
5.07665716731356
1.16863474789604
4.47635486149688
4.33050121578669
2.42974020115261
9.79494608790444
0.0568839453395247
22.9153086380666
4.48791386399205
59.6962194708933
97.8636220152072
1119.97978883924
806.144299041605
7.33252581243942
57.0699524267842
0.900104994068117
15.2791339483160
3.31266162202546
3.20809490583211
5.36617545130941
0.648122925703121
3.90480316969632
0.0338850542128927
2.58828964019220
0.543604662856673
1.16385064506181
1.01835324272839
0.172915006573539
1.55998411282069
0.00221570175453666
1.14803074836796
0.0769335878967426
0.421762398811163
0.468260146832541
0.203765185125597
0.467641715366303
0.00142988680149041
0.698088976126660
0.0413316717103625
0.190548157914037
0.504713663418641
0.325697764871308
0.375910057283262
0.123307135682793
0.331115262928959
0.00263961045860704
0.204555648718379
0.139008751575803
0.182936666944843
0.154943314848474
0.0840483576044629
0.293075999812128
0.00306911699543199
0.272993318570981
0.0864711337990886
0.280495615619829
0.0910123210559269
0.148399626645134
0.141945002415500
0.0512001531781583
0.0295283557338525
In MATLAB it is very easy to find peaks using findpeaks, like so:
[pxx_peaks,location] = findpeaks(Pxx);
If we plot pxx_peaks, we get
plot(pxx_peaks)
Of course, besides these peaks, there are smaller peaks which are not shown on the picture, but my goal is to find all peaks which are 95-96% above all other peaks.
I have tried like this:
>> average = mean(pxx_peaks);
>> stand = std(pxx_peaks);
>> final_peaks = pxx_peaks( pxx_peaks > average + 3*stand );
The result of this is
>> final_peaks
final_peaks =
1.0e+03 *
1.4222
1.4900
1.5104
1.1200
but how to return their corresponding locations? I want to write it as one m-file, so please help me
EDIT
also please help me in this question: can I parameterize the confidence interval? For instance instead of 95%, I want to find peaks that are 60% above then other peaks, is it possible?
Note that 3σ ≈ 99.73%
As for your first question, it's easy, you just have to keep track of the locations in the same way as you do for the peaks:
inds = pxx_peaks > mean(pxx_peaks) + 3*std(pxx_peaks);
final_peaks = pxx_peaks(inds);
final_locations = location(inds);
plot(Pxx), hold on
plot(final_locations, final_peaks, 'r.')
As for your second question, that's a little more complicated. If you want to formulate it like you say, you'll have to convert a desired percentage to the correct number of σ. That involves an integration of the standard normal, and a root finding:
%// Convert confidence interval percentage to number-of-sigmas
F = #(P) fzero(#(sig) quadgk(#(x) exp(-x.^2/2),-sig,+sig)/sqrt(2*pi) - P/100, 1);
% // Repeat with the desired percentage
inds = pxx_peaks > mean(pxx_peaks) + F(63)*std(pxx_peaks); %// 63%
final_peaks = pxx_peaks(inds);
final_locations = location(inds);
plot(final_locations, final_peaks, 'r.')