Matlab Min



Matlab Min

If one requires just the minimum intensity of an image, then min(Img (:)) would do the job; but if one is interested in the location of the intensity value, then the following code can be used:

Matlab minreal

Similarly, the maximum intensity of an image and its location can be obtained by maxlnt = max(Img(:) ) ; [r, c, z] = find(Img maxlnt);

Tsmin = min (tsobj) finds the minimum value in each data series in the financial time series object (tsobj) and returns it in the structure tsmin. The tsmin structure contains.

  1. 此 MATLAB 函数 返回数组的最小元素。 如果 A 是向量,则 min(A) 返回 A 的最小值。 如果 A 为矩阵,则 min(A) 是包含每一列的最小值的行向量。 如果 A 是多维数组,则 min(A) 沿大小不等于 1 的第一个数组维度计算,并将这些元素视为向量。此维度的大小将变为 1,而所有其他维度的大小保持不变。.
  2. Label Maximum and Minimum in MatLab Figure. Sometimes it is required to label the minimum and maximum values of a plot in a Matlab figure. The code snippet below demonstrate that functionality, for minimum and maximum values along the y-axis.% plot the data data= 2.5: 0.025: 10;% x-axis data fn1 = log( data.^ 2)./data + 0.035.data;% y.

TABLE 2.1

List of MATLAB functions for image arithmetic

Function

Description imabsdiff

Absolute difference of two images Add two images Complement an image Divide two images

Compute linear combination of two images Multiply two images Subtract two images imadd imcomplement imdivide imlincomb immultiply imsubtract

Note that there may be more than one pixel with the value of maxlnt in the image.

2.5.11 Image Arithmetic

Although image arithmetic can be performed using normal matrix operations, the Image Processing Toolbox includes special functions for it. Table 2.1 lists some such functions. Their main advantages, as mentioned in the MATLAB help pages, are

• No conversion to double data type is necessary. It should be noted that the operations are executed in double precision internally.

• Overflow is handled automatically

On Intel architecture processors, such functions can take advantage of the Intel Performance Primitives Library (IPPL), thus accelerating their execution time. The IPPL is a collection of basic functions used in signal and image processing.

2.5.12 Block Processing of Images

At times, block processing may be required when images exhibit some type of inhomogeneity, such as spatially varying noise characteristics, or when it is necessary to perform processing locally inside the image. In block processing, the image is divided into small distinct or overlapping blocks, and each block is processed separately. A nonuniform background or illumination is one of the best examples in which block processing may be helpful. Recall also that in Section 2.5.8.1, we performed pixel binning by using the block processing function. There are several functions in MATLAB that allow us to perform block processing: blkproc, im2col, col2im, nrfilter, and colfilt.

blkproc

Implement distinct block processing

im2col

Rearranges image blocks into columns

col2im

Reverses the operation of im2col,

rearranges the columns into blocks

nrfilter

Perform general sliding-neighborhood

operations

colfilt

Perform neighborhood operations

using columnwise functions

For the usage of these functions, the reader can refer to the MATLAB help pages.

2.6 Image Histogram

An intensity histogram represents the frequency of occurrence of intensities in an image. It is the approximation of the probability distribution function that intensities follow. Image histograms can be computed in several ways. For instance, the following statement

creates an image histogram in which nbins is the desired number of bins, and cbins denotes their centers. Figure 2.9 shows a synthetic image consisting of two normally distributed regions with means 10 and 20 and a standard deviation 2. Then the following code computes the histogram using 64 bins. The computed histogram is shown on the left of the Figure 2.9 using a bar plot.

Intensities

FIGURE 2.9

A gray-level image consisting of two normally distributed regions and its histogram on the right.

Intensities

FIGURE 2.9

A gray-level image consisting of two normally distributed regions and its histogram on the right.

Img = [normrnd(10,2,64,32), normrnd(20,2,64,32)]; [h,cbins]=hist(Img(:),64); bar(cbins,h,1);

The functions imhist and histc are the other alternatives in computing image histograms. For the usage of these functions, the reader can refer to MATLAB help pages.

Continue reading here: Histogram

Was this article helpful?

  • 2fminbnd
    • 2.1Examples
      • 2.1.1Different Function Calls
        • 2.1.1.1Most General Case
  • 3fminsearch
    • 3.2Examples

Introduction

This page discusses two different ways of getting MATLAB to find the minimum of a function (versus a data set) - fminbnd and fminsearch. The fminbnd command can find a single independent value that will minimize a one-dimensional function over a specific domain. The fminsearch command can find a single vector of values that will minimize a multi-dimensional function given some initial guess.

fminbnd

The fminbnd command in MATLAB can be used to find the value of a single parameter of a function that will minimize the value of the function on some bounded domain. The command can only find one minimum at a time and can only find minima based on one variable at a time. If there is a single local minimum over the domain, fminbnd should find it. If there are several, it should find one of them, though it may not find the most minimum of the minima. If there is no local minimum in the range, fminbnd will return one of the boundary values, depending on where the function is at its minimum value for the domain.There are several different ways to present fminbnd with the specific function and variable.

Examples

Different Function Calls

Most General Case

The following examples show a method that will work regardless of how many input variables your function has or how you define your function - whether it is built-in, a variable containing an anonymous function, an anonymous function generated on the fly, or a .m file function. The paradigm is:

where

  • INDEP_AT_MIN is the calculated value of the requested variable when the function is at a minimum in the domain
  • FUNCTION_MIN is the minimum value of the function at the INDEP_AT_MIN location
  • DUMMY_VAR is the variable you want to use in this FUNCTION_THING to indicate which of the various inputs fminbnd is allowed to alter
  • FUNCTION_THING can be a built-in function, the name of an anonymous function, the name of a .m file function, or a calculation - whatever it is that you are trying to minimize; note that DUMMY_VAR must appear somewhere in this expression for fminbnd to be able to do anything
  • LEFT_INDEP, RIGHT_INDEP represent the minimum and maximum value of the range over which you want to minimize the function.
Plot showing the function on the domain between -10 and 10

The example is based on wanting to determine minimum (and later, maximum) values of:

(f(x, y, z)=frac{x}{10}+cos(x)+sin(y)+z,!)

We will assume that two of the three variables ((y) and (z)) are known and that we want to find (x) given those values. We will further assume that (y=1) and (z=pi). A graph of this function for values of (x) between -10 and 10 is shown at right.

Function on the Fly

If you only want to solve this problem once, and the calculation only requires one line of code, the process with the least 'overhead' involves creating the function on the fly inside the fminbnd command. All you have to do is put your expression in for the FUNCTION_THING, making sure the DUMMY_VAR is appropriately used. You will also need to specify the boundary values.

Plot focusing on domain between 0 and 5 showing minimum at 3.0414

The line:

will produce

which has found the local minimum between (x=0) and (x=5).

Plot focusing on domain between 5 and 7 showing minimum at 5.0000
Matlab find lowest number in array

Running the code:

Matlab Mini

will produce

since, for (x) values between 5 and 7, there is no local minimum. The value of the function at 5 is less than the value of the function at 7, so the 5 is returned.

Plot focusing on domain between -9 and 10 showing 'minimum' at 3.0414
Plot focusing on domain between -4 and -2 showing minimum at -3.2418

Finally, for

MATLAB returns:

Note that this is not actually the 'minimum' minimum - that would be at (x=-3.2418), where the function is 2.6639, as given by

Furthermore, the actual minimum value of the function over the domain from -9 to 10 is given at (x=-9), where (f=2.1719); since MATLAB was able to find at least one local minimum, however, it did not check the boundary values.

A slightly 'longer' version of this would have you pre-define the (y) and (z) variables first, then use the variables in the function definition:expression in for the FUNCTION_THING, making sure the DUMMY_VAR is appropriately used. The line:

This works the same as the last example above - recall that when anonymous functions are created, they can take 'snapshots' of the workspace. In the case above, the FUNCTION_THING is able to 'see' what the variables y and z contain.

Pre-Defined Functions

If you want to solve the same problem multiple times, perhaps by altering initial guesses or altering one or more of the constant parameters in the function, you should first define the function. If the expression can be calculated all on one line, you may choose to use a variable with an anonymous function - for example:

On the other hand, if the expression is more complicated or if you just want to put it in a .m file, you can certainly do that as well. For example, instead of the MyFun variable above, you could write a MyFun.m file containing:

In either case, you would use fminbnd by properly building the FUNCTION_THING using the name of the function. As in the previous example, you can hard-code the values for the (x) and (y) values:

or you can use variables to transmit the information:

In any event, note again that the FUNCTION_THING only has one unknown, and that unknown is named as the DUMMY_VAR.

Single-Variable Anonymous Functions

For single-variable anonymous functions, there is a slightly simpler way to run fminbnd - all you need to do is give the name of the variable to FUNCTION_THING rather than setting up the DUMMY_VAR. For example, to find the minimum value of (x^2-cos(x)) on the domain from (x) going from -2 to 3, you can set up an anonymous function for it as:

and then solve with:

to get

Single-Variable Built-in Functions and .m Functions

The following method works for both built-in single-input functions and .m file single-input functions (i.e. not variables containing anonymous functions). The first argument is the name of the function in single quotes and the second argument is the initial guess or initial bracket for the one variable of the function. For example, to find the minimum value of (cos(x)) for the domain of (x) going from 0 to 4, you can type

and get

If you have a .m file function of one variable, the process is the same. For example, to find the minimum value of (y=x^2-cos(x)) on a domain from -2 to 3, you could first create a .m file called MyCalc.m with:

then use fminbnd:

and get

Finding Maximum Values

Plot focusing on domain between 5 and 7 showing maximum at 6.3834

There is no fmaxbnd command. Conveniently, all you need to do to figure out the value and location of the maximum values would be to give fminbnd your function multiplied by -1. The only problem here is that the value of the function fminbnd returns will be the opposite of what you want. For example:

will return

in order to get the actual value of the function at a maximum, you would need to re-run the calculation or just create a new variable that is equal to -1 multiplied by the function value MATLAB gave:

to get

Where the Mins Are

As noted above, if you give MATLAB a domain with multiple local minima, it will not always find the most-minimum minima; here are two graphs showing 19,900 runs of fminbnd with different boundaries. The x axis represents the left boundary and the y axis represents the right boundary.

The black regoins indicate left and right values that will not work since 'Right' would be to the left of 'Left' there.There are four local minima over the entire domain from -10 to 10. In the left graph, with the locations, these are represented by

  • Dark blue: (-9.5250, 2.0356)
  • Cyan: (-3.2418, 2.6639)
  • Yellow: (3.0414, 3.2922)
  • Red: (9.4246, 3.9205)

In the right graph, with the values of the minimum, these are represented by:

  • Dark blue: (-9.5250, 2.0356)
  • Medium blue: (-3.2418, 2.6639)
  • Cyan: (3.0414, 3.2922)
  • Green: (9.4246, 3.9205)

For large enough - or well-placed enough - domains, MATALAB will find one of the local minima; if the domain does not span a minimum, it will report back the minimum value at either the left or right boundary. If the domain spans multiple minima, it will report back one of the minima, though not always the minimum one. Notice when the domain is [-10 -4] the dark blue minimum is found, but when the domain is [-10 0], the cyan/medium blue minimum at -3.2418 is found -- even though its value (2.6639) is greater than the value at -9.5250. These graphs show that you need to make sure your domain is large enough to include the minimum you are trying to find but not so large as to include multiple minima.

The code for making these graphs is given in the expandable box here; click the 'Expand' link at right.

Matlab Min Index

Matlab


fminsearch

The fminsearch command in MATLAB can be used to find the value of a single vector input of a multivariable function that will minimize the value of the function on some unbounded domain. The command can only find one minimum at a time and can only find minima based on one variable at a time - but that variable can be a vector instead of a single entry.

Most General Case

The following examples show a method that will work regardless of how many input variables your function has or how you define your function - whether it is built-in, a variable containing an anonymous function, an anonymous function generated on the fly, or a .m file function. The paradigm is:

where

  • INDEP_AT_MIN is the calculated value of the requested variable when the function is at a minimum in the domain
  • FUNCTION_MIN is the minimum value of the function at the INDEP_AT_MIN location
  • DUMMY_VAR is the variable you want to use in this FUNCTION_THING to indicate which of the various inputs fminbnd is allowed to alter
  • FUNCTION_THING can be a built-in function, the name of an anonymous function, the name of a .m file function, or a calculation - whatever it is that you are trying to minimize; note that DUMMY_VAR must appear somewhere in this expression for fminbnd to be able to do anything
  • INIT_GUESS represents an initial guess for where the minimum value is on the surface. There need to be as many values in the initial guess as there are entries in the dummy variable.
Matlab Min

Examples

One Value

If you want to find the location of the minimum of some function (f(x)) where (x) is a single value (a scalar), all you need is a function and an initial guess. For example, to find the minimum of:

(f(x)=(x+3)(x)(x-1)(x-3))

you can run:

and MATLAB will find a minimum of (f(-2.0234)=-30.01). Note, however, if you make an initial guess of 1:

will return the local - but not global - minimum of (f(2.2873)=-11.10). Since there is no way to force fminsearch to find a minimum within a particular set of boundaries, either you should use fminbnd if you only have one parameter or you should be extremely careful in picking your initial conditions. The code below demonstrates that MATLAB finds different locations and values for the minimum depending on initial condition:

Multiple Value Vector

Imagine if you were to want to find the minimum of some function

(f(x, y)=(x-1)^2+(y-2)^2,!)

which has a discernible minimum at (1, 2); you could use:

to have MATLAB start at the origin and find the value of the vectorrDummy that has the x and y coordinate in it.Since this function only has one minimum, MATLAB should be able to find it as long as the initial guess is somewhat reasonable.


Questions

Post your questions by editing the discussion page of this article. Edit the page, then scroll to the bottom and add a question by putting in the characters *{{Q}}, followed by your question and finally your signature (with four tildes, i.e. ~~~~). Using the {{Q}} will automatically put the page in the category of pages with questions - other editors hoping to help out can then go to that category page to see where the questions are. See the page for Template:Q for details and examples.

External Links

References

Retrieved from 'http://pundit.pratt.duke.edu/piki/index.php?title=MATLAB:Fminbnd_and_fminsearch&oldid=23117'