Filter löschen
Filter löschen

Help with index for maximum value

2 Ansichten (letzte 30 Tage)
Brantosaurus
Brantosaurus am 14 Feb. 2024
Kommentiert: Brantosaurus am 14 Feb. 2024
I have an 2D array of data containing product fractions. The column index is associated with a mix ratio. The row index is associated with the product species. I can find the maximum fraction value for a specific species, but the mix ratio (and index) is wrong.
My code gives:
species max-frac mix-ratio
*H2 0.96220083 0.3
H2O 0.6870072 6.5
*OH 0.12253145 8
*O2 0.098079432 10
*H 0.041959177 10
*O 0.023207462 10
HO2 0.00045286 10
H2O2 6.51818E-05 10
O3 1.13457E-06 10
But the correct answer should be:
species max-frac mix-ratio
*H2 0.96220083 0.3
H2O 0.6870072 8
*OH 0.12253145 10
*O2 0.098079432 10
*H 0.041959177 6.5
*O 0.023207462 10
HO2 0.00045286 10
H2O2 6.51818E-05 10
O3 1.13457E-06 10
Any help sorting this out would be much appreciated.
format long g
fracts()
species max-frac mix-ratio *H2 9.6220083e-01 3.0000000e-01 H2O 6.8700720e-01 6.5000000e+00 *OH 1.2253145e-01 8.0000000e+00 *O2 9.8079432e-02 1.0000000e+01 *H 4.1959177e-02 1.0000000e+01 *O 2.3207462e-02 1.0000000e+01 HO2 4.5285974e-04 1.0000000e+01 H2O2 6.5181832e-05 1.0000000e+01 O3 1.1345688e-06 1.0000000e+01
  2 Kommentare
Walter Roberson
Walter Roberson am 14 Feb. 2024
Bearbeitet: Walter Roberson am 14 Feb. 2024
type fracts
of = [0.3 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5 8 8.5 9 9.5 10]; names = { '*H2'; 'H2O'; '*OH'; '*O2'; '*H'; '*O'; 'HO2'; 'H2O2'; 'O3'}; fracs = [ 0.96220083253122 0.937001387552033 0.874002714042574 0.810992700049054 0.74780577262337 0.683840005856851 0.618357080450297 0.551422328259094 0.484287900480256 0.418884308578777 0.357182267236621 0.300826351044237 0.250956482728154 0.208114480958745 0.172238875222256 0.142774189736819 0.118866074281767 0.0995650622968137 0.0839741805776359 0.0713232035303872 0.0609871740687707; 0.0377991674687795 0.0629986124479674 0.125997220790509 0.188994457804212 0.25195480252116 0.314622580031103 0.376181736506199 0.43517993283166 0.489930222331301 0.538953613901196 0.581167160588662 0.615930444659731 0.643073504795639 0.66290507957213 0.676152906227016 0.683821047289943 0.687007202392198 0.686746500964421 0.683921193935693 0.67923402113771 0.673221297606501; 2.22327976852125e-24 6.85759953134214e-18 1.24360881321903e-10 1.65983550999297e-07 9.48040334378583e-06 0.000128094622628151 0.000771840329195554 0.00279738011490611 0.00723162124064597 0.0148176596665369 0.0257038536127702 0.0393457993900356 0.0546117690529415 0.070076854609197 0.0844073534257148 0.0966629355644787 0.106397607625811 0.11357926910949 0.118432863790443 0.121297320925404 0.122531452149732; 0 9.21364685054006e-32 5.34174433004885e-19 1.89295142164211e-13 2.68543345763431e-10 3.04085360475654e-08 8.38270610050516e-07 9.54211215508342e-06 6.06164189804761e-05 0.000258760896008051 0.000832935663080812 0.00217119193598601 0.00479493196399169 0.00925026325203391 0.0159515556041904 0.025061794920379 0.0364737499057485 0.0498818174109408 0.06488555384713 0.0810748040992137 0.098079431948234; 3.34821507154084e-18 2.52865047941693e-13 6.51669170636362e-08 1.26761631833543e-05 0.000229944452125781 0.00140912960718508 0.00468506011821732 0.0105601254956405 0.018335667277251 0.0265638781664281 0.0337757085317884 0.0389383878973618 0.0416222109937517 0.04195917716899 0.0404567590241403 0.0377567111864494 0.0344415242874894 0.0309413222646071 0.0275266078132032 0.024343115131064 0.0214540059464803; 0 2.09994239128734e-30 7.47501118128108e-18 1.80187954464638e-12 1.86002251374892e-09 1.59473697335477e-07 3.40470296396595e-06 3.03976169384125e-05 0.000152567015120172 0.000516894635128139 0.00132474469856626 0.00275772447439952 0.0048827991380266 0.00759473728588379 0.0106401124308879 0.0137091378038229 0.0165335172395724 0.0189393643993122 0.0208497082710153 0.0222597310944603 0.0232074621430668; 0 6.15700192926503e-34 5.59172768866557e-21 2.30027012028065e-15 3.46998695200764e-12 4.01572114685042e-10 1.10327080890718e-08 1.22718487457335e-07 7.49217760837377e-07 3.03052602174162e-06 9.13292173694356e-06 2.20669406641223e-05 4.48216072210106e-05 7.9100015422758e-05 0.000124413872247518 0.000178163425558326 0.000236584185151753 0.000295876561847102 0.000352953824076108 0.000405693385509828 0.000452859736165154; 9.5429764863276e-35 1.63464631004911e-26 3.1311605154588e-17 3.60763856556982e-13 7.52658165719256e-11 2.47087579926888e-09 2.85898094276133e-08 1.70851118629214e-07 6.56018684470211e-07 1.85362990389827e-06 4.19674677437665e-06 8.03365758476776e-06 1.34678983749615e-05 2.02726866956291e-05 2.79423159407678e-05 3.58554678020406e-05 4.34511487430121e-05 5.03319961748204e-05 5.62805544117132e-05 6.12234322683563e-05 6.51818322320969e-05; 0 0 1.15144522820615e-36 7.42646875633622e-27 2.67068010341209e-21 1.10256114042769e-17 3.76650264552703e-15 2.69148029824262e-13 6.80746887494861e-12 8.43324431369168e-11 6.26559084359002e-10 3.16784106306547e-09 1.18218999340825e-08 3.44509017852597e-08 8.187760648436e-08 1.64604748359476e-07 2.8893351894724e-07 4.54996393304191e-07 6.57386391534331e-07 8.87263983275629e-07 1.13456881751286e-06 ]; maxFracs = max(fracs,[],2); [~,c] = find(fracs == maxFracs); ofMaxFracs = of(c)'; fmt = '%16s %16.7e %16.7e\n'; data = [names num2cell(maxFracs) num2cell(ofMaxFracs)].'; var = '\n species max-frac mix-ratio\n'; fprintf(var) fprintf(fmt,data{:}) %{ code gives this species max-frac mix-ratio *H2 0.96220083 0.3 H2O 0.6870072 6.5 *OH 0.12253145 8 *O2 0.098079432 10 *H 0.041959177 10 *O 0.023207462 10 HO2 0.00045286 10 H2O2 6.51818E-05 10 O3 1.13457E-06 10 %} %{ correct answer is species max-frac mix-ratio *H2 0.96220083 0.3 H2O 0.6870072 8 *OH 0.12253145 10 *O2 0.098079432 10 *H 0.041959177 6.5 *O 0.023207462 10 HO2 0.00045286 10 H2O2 6.51818E-05 10 O3 1.13457E-06 10 %}
Brantosaurus
Brantosaurus am 14 Feb. 2024
Thank you for the formatting Walter. I'll try better next time!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Voss
Voss am 14 Feb. 2024
The easiest way to fix this is to use the second output from max, which tells you the column index in frac of each element of maxFracs.
[maxFracs,c] = max(fracs,[],2);
ofMaxFracs = of(c)';
  4 Kommentare
Voss
Voss am 14 Feb. 2024
You're welcome!
Brantosaurus
Brantosaurus am 14 Feb. 2024
Also thanks for your help explaining my mistake. Very valuable. I'll keep that for reference.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by