find the max value in an array from a selection of elements corresponding to non unique items in another array

4 Ansichten (letzte 30 Tage)
I have two arrays lets call them x and y and if you plot them you can create the following plot for example
As you can see there are some x values that have two corresponding y values. In two arrays of data (Example data below) this would be that there are two occurances of a particular x value (e.g. x = -350) each with two different y values (y = 1.331573626327333 & 43.073736543265510)
How do I:
1) remove all duplicate x data (this I think is the easier part using the unique function I guess). I still want there to be one of these occurances to remain in the arry however at the end
2) find only the max value from the corresponding y data. (i.e from the previous example for x = -350 it should only return y= 43.073736543265510)
In other words how do I remove the duplicate data with the min y value, leaving the max y value and one iteration of the x value behing?
x = [-344.889995378401 -345 -346 -347 -348 -348.790577839601 -349 -350 -351 -352 -352.364199727765 -353 -354 -355 -355.649037035211 -356 -357 -358 -358.675037800818 -359 -360 -361 -361.473920026108 -362 -363 -364 -364.064720071035 -365 -366 -366.456572583127 -367 -368 -368.663505839183 -369 -370 -370.696656851489 -371 -372 -372.565575585720 -373 -374 -374.278385142883 -375 -375.841868323049 -376 -377 -377.261734208111 -378 -378.542802425223 -379 -379.689019976183 -380 -380.703560818727 -381 -381.588917426414 -382 -382.346929864271 -382.978818186504 -383 -383.485197007167 -383.866099638873 -384 -384.120939445921 -384.248508431993 -384.246975028648 -384.113807290917 -384 -383.845642078457 -383.438301912707 -383 -382.886839407149 -382.184796216022 -382 -381.324723768083 -381 -380.297652670582 -380 -379.092744072702 -379 -378 -377.696307208119 -377 -376.092941049423 -376 -375 -374.262056927059 -374 -373 -372.179721696529 -372 -371 -370 -369.814388394383 -369 -368 -367.126138426752 -367 -366 -365 -364.061760966373 -364 -363 -362 -361 -360.547531003596 -360 -359 -358 -357.518863898406 -357 -356 -355 -354.137518769451 -354 -353 -352 -351 -350.304827964543 -350 -349 -348 -347 -346 -345.903207469402 -345 -344 -343 -342 -341 -340.738362769946 -340 -339 -338 -337 -336 -335 -334.470451478080 -334 -333 -332 -331 -330 -329 -328 -327 -326.358673906962 -326 -325 -324 -323 -322 -321 -320 -319 -318 -317 -316 -315 -314.826864645813 -314 -313 -312 -311 -310 -309 -308 -307 -306 -305 -304 -303 -302 -301 -300 -299 -298 -297 -296 -295 -294 -293 -292 -291 -290 -289 -288 -287 -286 -285 -284 -283 -282 -281 -280 -279 -278 -277 -276 -275 -274 -273 -272 -271 -270 -269 -268 -267 -266 -265.485627509798 -265 -264 -263 -262 -261 -260 -259 -258 -257 -256 -255 -254 -253 -252.121489198760 -252 -251 -250 -249 -248 -247 -246 -245 -244 -243 -242.490780588105 -242 -241 -240 -239 -238 -237 -236 -235 -234.195033116041 -234 -233 -232 -231 -230 -229 -228 -227.024343121247 -227 -226 -225 -224 -223 -222 -221 -220.759814889434 -220 -219 -218 -217 -216 -215.160349441984 -215 -214 -213 -212 -211 -210.083517539267 -210 -209 -208 -207 -206 -205.435236196409 -205 -204 -203 -202 -201 -200 -199.792746246760 -199 -198 -197 -196 -195 -194.611481636899 -194 -193 -192 -191 -190 -189.914617322129 -189 -188 -187 -186 -185.638250087510 -185 -184 -183 -182 -181.732061007438 -181 -180 -179 -178.158996830696 -178 -177 -176 -175 -174.889620754774 -174 -173 -172 -171.900243401682 -171 -170 -169.171620462662 -169 -168 -167 -166.687813819363 -166 -165 -164.435590811588 -164 -163 -162.403939292291 -162 -161 -160.583618370755 -160 -159 -158.998264706511 -158 -157.624876721980 -157 -156.439819509359 -156 -155.437922250342 -155 -154.614791184103 -154 -153.966796154046 -153.491067405984 -153.185299106022 -153.047864406426 -153.077703857249 -153.274340321440 -153.637873235383 -154 -154.168982324284 -154.868978248684 -155 -155.739836605355 -156 -156.784186286643 -157 -158 -158.005433020272 -159 -159.408133515186 -160 -161 -161.048957777685 -162 -162.926843921165 -163 -164 -165 -165.020663819739 -166 -167 -167.341769256470 -168 -169 -169.902790859422 -170 -171 -172 -172.720778044628 -173 -174 -175 -175.815150082974 -176 -177 -178 -179 -179.211325317224 -180 -181 -182 -182.940568698066];
y = [0 0.0272194650481636 0.277667211041931 0.532514056802643 0.791783561400847 1 1.05684019501265 1.33157362632733 1.61101822986139 1.89519567559844 2 2.18919049076846 2.49106573632331 2.79797974637965 3 3.11334931574240 3.44027028255356 3.77255346864151 4 4.11402929910814 4.46902724886179 4.82787908226978 5 5.19975498674158 5.58417032438329 5.97451111232560 6 6.38671247257059 6.80627428865191 7 7.24316435907907 7.69589300518693 8 8.16344613304419 8.65404361736447 9 9.16051123132827 9.69462021996493 10 10.2514461475017 10.8358956023243 11 11.4590770672394 12 12.1105214441594 12.8143840872053 13 13.5745646312312 14 14.3975115697007 15 15.3056046637972 16 16.3341641298170 17 17.5419124747450 18 19 19.0418704364112 20 21 21.5263245578540 22 23 24 25 25.4268397264698 26 27 27.7969847727076 28 29 29.2177462528981 30 30.3202872961235 31 31.2509646326650 32 32.0679034389998 32.7865310672453 33 33.4405799616769 34 34.0521996389837 34.6037917902708 35 35.1294645884743 35.6136417140000 36 36.0785835714883 36.5075819028805 36.9241869322799 37 37.3113507163817 37.6835947011485 38 38.0430382177564 38.3778880260209 38.7032572137306 39 39.0185268065508 39.3130007248546 39.5991579928548 39.8771499173972 40 40.1830422689700 40.5192777579917 40.8462773686695 41 41.1599786300243 41.4612082087638 41.7541181177522 42 42.0379634394675 42.3084492255619 42.5714047817752 42.8269572729572 43 43.0737365432655 43.3101697340312 43.5398685678795 43.7629453495439 43.9795109723238 44 44.1864753289209 44.3870322263417 44.5816329362551 44.7703752566327 44.9533556688765 45 45.1288214088546 45.2982245943236 45.4623250054285 45.6212074568217 45.7749555156692 45.9236514991676 46 46.0666070717566 46.2039131263162 46.3365360895456 46.4645479109286 46.5880193496423 46.7070199716128 46.8216181465068 46.9318810446918 47 47.0375519144161 47.1385085903986 47.2354079480332 47.3283081312105 47.4172661525547 47.5023378923477 47.5835780976915 47.6718792924470 47.7564066942133 47.8372108703701 47.9143413448562 47.9878465951739 48 48.0574711227212 48.1235441947623 48.1861743936172 48.2454031037989 48.3012707352430 48.3538167239141 48.4030795328584 48.4490966537213 48.4919046087505 48.5315389532986 48.5680342788442 48.6014242165427 48.6317414413225 48.6590176765356 48.6832836991738 48.7045693456577 48.7229035182052 48.7383141917832 48.7508284216473 48.7604723514679 48.7672712220438 48.7712493805999 48.7724302906644 48.7708365425201 48.7664898642203 48.7594111331615 48.7496203881993 48.7371368422962 48.7219788956855 48.7041641495352 48.6837094200941 48.6606307533013 48.6349434398378 48.6066620306000 48.5758003525696 48.5535703042663 48.5287210639115 48.5012644662994 48.4712116477520 48.4385730607766 48.4033584891069 48.3655770631041 48.3252372754916 48.2823469973981 48.2369134946827 48.1889434445154 48.1384429521841 48.0854175681021 48.0298723049881 48 47.9718115636930 47.9112390810900 47.8481577341391 47.7825697442610 47.7144768382988 47.6438802685785 47.5707808329560 47.4951788948193 47.4170744030137 47.3364669116620 47.2533555998496 47.1677392911466 47.0796164729409 47 46.9889812545793 46.8957995579050 46.8000928504331 46.7018570703232 46.6010878977656 46.4977807751645 46.3919309269136 46.2835333787455 46.1725829766356 46.0590744052461 46 45.9429364851299 45.8241421246834 45.7027468174559 45.5882964290872 45.4711205683133 45.3512095199400 45.2285534357784 45.1031423530113 45 44.9749122615648 44.8436592146970 44.7095806981797 44.5726645365814 44.4328985420634 44.2902705310962 44.1447683404729 44 43.9963677580251 43.8445527975371 43.6897713580103 43.5320095155122 43.3712534719147 43.2074895678946 43.0407042951559 43 42.8702663159264 42.6965201391615 42.5196388030051 42.3396075906558 42.1564120357951 42 41.9698545760485 41.7790877199458 41.5850244544371 41.3876495413917 41.1869480858613 41 40.9827749606938 40.7737491260170 40.5612465007405 40.3452518983285 40.1257505560649 40 39.9260346433899 39.7537701183348 39.5788214987752 39.4011803265135 39.2208385408334 39.0377884746398 39 38.8525603906248 38.6636342892800 38.4715857330046 38.2764017798323 38.0780699235051 38 37.8741808302773 37.6654370189710 37.4533168838962 37.2378081693135 37.0188991010721 37 36.7918166203136 36.5606492881030 36.3258217285174 36.0873229135416 36 35.8408772973550 35.5881010800979 35.3313597406587 35.0706434384327 35 34.7996974484472 34.5222041878318 34.2404048673906 34 33.9525975512307 33.6512897736333 33.3453020424605 33.0346275583549 33 32.7072211953442 32.3732382950445 32.0341475482132 32 31.6747177910007 31.3080390215111 31 30.9321851442425 30.5330813061153 30.1278465302857 30 29.6983322320471 29.2539081298296 29 28.7882948934346 28.2966780669712 28 27.7805728539791 27.2312628972327 27 26.6419536782021 26.0011222355206 26 25.2755952074750 25 24.4754227121580 24 23.5633366413415 23 22.4699620773531 22 21.0515570260791 21 20 19 18 17 16 15 14.3174544687247 14 13 12.8487603209348 12 11.7494527763937 11 10.8219534165399 10.0044072227190 10 9.28874231909957 9 8.62467937399871 8.02890761546989 8 7.48984573298377 7 6.96447707427975 6.48331753810917 6.00969728657976 6 5.57352625067553 5.14482829921361 5 4.73898259939742 4.34777330518609 4 3.96470024329579 3.60517577581750 3.25136002891266 3 2.90758926505897 2.58039885768803 2.25845522835426 2 1.94404243000721 1.64468881274853 1.35017131473224 1.06048592890052 1 0.783242371444436 0.512542005507627 0.246305822720229 0];
  3 Kommentare
Nick Turton
Nick Turton am 4 Nov. 2019
This has partially done what I need it to do as it is reloving duplicates but doesn't appear to remove the duplicates I want to remove.
I want to remove y values after xmax/xmin but only the duplicated values with the minimum value of y.
If I was to try this for the example data above and then search for the corresponding value of y for x = -350 it would now return y = 1.331573626327333, not the y = 43.073736543265510 which I desire
vals_y(find(vals_x==-350))

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Guillaume
Guillaume am 4 Nov. 2019
Note: For such large arrays, it is better to attach your data as a mat file rather than copy/pasting the content in the question. In particular, by pasting the data as text, you inevitably introduce some rounding of the values which may have important implication to what you want.
Your x data is made up of floating-point values. You always have to be very careful when you say that two values are equal in an array of floating-point numbers. For example:
>> x = [0.1+0.1+0.1, 0.3]
x =
0.3 0.3
Are the two values in x the same? You'd think so
>> x(1) == x(2)
ans =
logical
0
but actually, they aren't.
>> x(1)-x(2)
ans =
5.5511e-17
They differ by a very small amount. The same might be the case for your duplicate values. For that reason, comparison with floating point numbers should always be done by comparing their difference relative to a small tolerance. Fortunately, for you, matlab has uniquetol that simplifies this.
So, to answer your question:
tol = 3e-3; %seems to work fairly well with your data
[xvals, ~, loc] = uniquetol(x); %get unique values of x and their location
yvals = accumarray(loc, (1:numel(y))', [], @(locs) max(y(locs))); %get max y at corresponding locations
Note that if you reduce the tolerance, eg.
tol = 1e-5;
[xvals, ~, loc] = uniquetol(x, tol);
yvals = accumarray(loc, (1:numel(y))', [], @(locs) max(y(locs)));
plot(x, y, xvals, yvals)
then you get
notunique.png
that's because some x elements are not actually duplicate as I explained, and some of these unique values correspond to the 'bottom' part of your graph.
Perhaps, a better solution is to go back to the equation that generated your data and limit the range before computing the result.
  1 Kommentar
Nick Turton
Nick Turton am 5 Nov. 2019
Thanks everyone for your responses. I'll take your comments on board for furure reference.
BTW I have found a hacky workaround to my issue for this occasion
[val_x_min, loc_x_min] = min(x);
y_at_xmin = y(loc_x_min);
[val_x_max, loc_x_max] = max(x);
y_at_xmax = y(loc_x_max);
y_linear = (y_at_xmin-y_at_xmax)/(val_x_min-val_x_max).*(x-val_x_min) + y_at_xmin;
y2= y-y_linear;
D= [x;y2];
D(:, D(2,:) < 0) = [];
D(2,:) = D(2,:) +(y_at_xmin-y_at_xmax)/(val_x_min-val_x_max).*(D(1,:)-val_x_min) + y_at_xmin;
plot(x,y,D(1,:),D(2,:),'lineWidth',2)
untitled1.png

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Debugging and Analysis finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by