Matlab command question: length max([x+y-1,x,y])

In summary, the conversation discusses the use of the length command in MATLAB and how it returns the number of characters in a string. The command length max([2+3-1,2,3]) is equivalent to length 'max([2+3-1,2,3])', which has 16 characters. However, if parentheses are used, the command evaluates to max([4, 2, 3]) which returns a double precision number of 4 and has a length of 1.
  • #1
PainterGuy
940
69
Hi,

I'm not sure if I should be asking this but I couldn't make sense of the following Matlab command: length max([2+3-1,2,3]). The result is "16". I couldn't the operation of this command and why it produces the result of "16". Could you please help me with?
 
Physics news on Phys.org
  • #2
The length command, when used in the way you have shown, returns the number of characters in the string after it. So, for example, length ans returns 3 because the number of characters in ans is 3. Similarly, the number of characters in max([2+3-1,2,3]) is 16, so the command length max([2+3-1,2,3]) returns 16.

Perhaps you are trying to use length(max([2+3-1,2,3]))?

https://in.mathworks.com/help/matlab/ref/length.html
 
  • Like
Likes PainterGuy
  • #3
Wrichik Basu said:
Similarly, the number of characters in max([2+3-1,2,3]) is 16, so the command length max([2+3-1,2,3]) returns 16.

I'm sorry but how the number of characters is 16. The command max([2+3-1,2,3]) produces the result 4.
 
  • #4
PainterGuy said:
I'm sorry but how the number of characters is 16. The command max([2+3-1,2,3]) produces the result 4.
The command max([2+3-1,2,3]) is not executed. It is considered a string, and MATLAB gives you the number of characters in that string.
 
  • Like
Likes PainterGuy
  • #5
When you use length without parentheses, it assumes that everything after it is a string, so
length max([2+3-1,2,3])
is really the same as
length 'max([2+3-1,2,3])'
and the string 'max([2+3-1,2,3])' is 16 characters long.

If you would have used parentheses, it would not make the assumption that the argument is a string. It would evaluate the string. And max([2+3-1,2,3]) evaluates to max([4, 2, 3]) which evaluates to a double precision number 4. And length(4) is, of course, 1. So if you do
length(max([2+3-1,2,3]))
you'll get 1

Not really sure what you want, but those are two ways to proceed. Adapt it to get what you need or want.
 
  • Like
Likes PainterGuy

1. What does the "length" command do in Matlab?

The "length" command in Matlab returns the number of elements in a vector or the number of characters in a string.

2. What is the purpose of the "max" command in Matlab?

The "max" command in Matlab is used to find the maximum value in a vector or matrix.

3. How does the "length" command work with the "max" command in Matlab?

The "length" and "max" commands can be used together to find the maximum value in a vector or matrix and return the number of elements in that vector or matrix.

4. What does the square brackets "[ ]" mean in the "max" command?

The square brackets in the "max" command indicate that the values inside them will be evaluated as a single matrix, and the maximum value will be returned.

5. What is the purpose of the "-" symbol in the "max" command?

The "-" symbol in the "max" command is used to subtract the values inside the square brackets before finding the maximum value.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
16
Views
13K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
Back
Top