简体   繁体   中英

matlab max function explanation

Can somebody explain me what the following bit of code in Matlab is doing?

S = max(Smax/1e6,S);

Where Smax is a scalar and S is a 2d array.

In the documentation it says that: "C = max(A,B) returns an array the same size as A and B with the largest elements taken from A or B. The dimensions of A and B must match, or they may be scalar."

But I don't really get it. For example when I look at the data and: Smax = 18.8919 then if the table cell = 0 it gets substituted by 1.88918608566891e-05 if the table cell = 1.26794177568026 then it gets substituted by 0.2374. Why is that happening?

Matrix before:

0   0   0   0   0   0.00111506400432957 0.0530528652679544  0.0735632798530057  0.337940336906895   0.217390264464039   0.842200681286881   0.441492820596403   0.635889579407697   0.0282173990214626  0.628686747522517
0   0   0   0   0   0.00124231354963584 0.0292268862938039  0.307854738413881   0.642250450652130   0.895774237272530   0.955937999893653   1.02416313393959    1.43102268912588    1.28612042237543    0.854476003698339
0   0   0   0   0   0.00161382073055088 0.211968938444796   1.14984433303987    1.79814921306101    1.21125341085802    0.730742783435531   0.783994053903355   2.16496222396151    1.37726874308377    0.566007297543274
0   0   0   0   0   0.00219642315401969 0.333759445110180   1.41975817872937    2.93443500804371    0.423882373725561   1.72297295599714    2.10661511095899    1.23659139050992    0.299608689818999   1.27117497124802
0   0   0   0   0   0.00293437095181858 0.170826340853254   0.505706488974208   3.09277881791955    1.01375749953753    2.72388777599623    2.14043273302288    1.97540038269762    1.36646318061577    1.31241005396504



 Matrix after:

1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05
1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    0.0238758244166261  0.358389355882494   0.251630262469816   1.88918608566891e-05
1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    0.139626570638794   0.586757920960373   0.191655700200727   1.88918608566891e-05    1.88918608566891e-05    0.772402912785602   0.320102366342357   1.88918608566891e-05
1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    0.350486560441589   1.07651493323094    1.88918608566891e-05    0.544051261541669   0.745082446545145   0.212358715890953   1.88918608566891e-05    0.239941646971664
1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.88918608566891e-05    1.12906998061232    0.0136637242376597  1.00206018919932    0.761008020284666   0.680771103223579   0.312225781724240   0.271865182822984

First of all, it's common practice to put the scalar variable as the second argument to the matrix, making it a bit easier to read.

To illustrate how this should work, I'll give you a bit of code:

R = [0 1; 2 3];
Smax=1;
S=max(R,Smax)
% At this point, S will be equal to [1 1; 2 3];

What will happen is that any value below Smax will be replaced by Smax.

In reality, this acts more like a minimum, ie, the minimum value is Smax.

As for your specific problem, I could get more info, but I would need to see what the matrix looks like before and after the statement you provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM