Matlab: What does the second argument in max (A, [], dim) mean?

I just used the max function in a specific column of the matrix, and the syntax made me wonder:

What does this empty matrix pass as the second argument?

 max(A,[],dim) 

I know this, probably in order to separate it from max(A,i) , which makes a comparison. But why is the matrix empty?

Does it have a certain meaning? Is this argument used in other functions like this?

+4
source share
3 answers

Actually, your guess is correct. Since Matlab is not a strongly typed language and there is no classic overload function, the function must be aware of the value of the context argument. Mathworks wanted to combine both maxima in one matrix and along two arrays in one function.

Therefore, they must somehow separate these cases. And they use the empty matrix [] as a placeholder. Otherwise, they will not be able to separate the cases max(A, dim) and max(A, B) . For this purpose they could use any special variable, but [] is a fundamental construction.

+8
source

This allows you to compare two matrices of equal size to find an element with max. See docs . Using [] as an input is just a way to jump to a later input.

+2
source

The max function compares pairs of default values. Entering max(1,2) obviously outputs 2 . As another example, using max(x,0) is a neat way to perform half-wave rectification of x by comparing each value of x with a single value of 0 . To compare the elements of one matrix with the values ​​contained in this matrix, the second argument can be specified as an empty matrix [] . It acts as a function flag for comparing values ​​within a single matrix.

You will see the empty value [] used in many Matlab functions throughout the documentation. This often indicates that the default value should be used or changes the mode of operation of the function.

+1
source

Source: https://habr.com/ru/post/1484773/


All Articles