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

  • Context: MATLAB 
  • Thread starter Thread starter PainterGuy
  • Start date Start date
  • Tags Tags
    Length Matlab
Click For Summary

Discussion Overview

The discussion revolves around understanding the behavior of the Matlab command "length max([x+y-1,x,y])", specifically why it produces the result "16". Participants explore the implications of using the length command without parentheses and the interpretation of the max function in this context.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about the command and its output, seeking clarification on why "length max([2+3-1,2,3])" results in "16".
  • Another participant suggests that the length command returns the number of characters in the string representation of the command, implying that "max([2+3-1,2,3])" is treated as a string.
  • A different participant challenges the claim that the number of characters is 16, stating that the actual output of "max([2+3-1,2,3])" is 4.
  • Further clarification is provided that without parentheses, the length command interprets the following input as a string, thus counting characters rather than evaluating the expression.
  • One participant explains that using parentheses would change the evaluation, leading to a different output, specifically that "length(max([2+3-1,2,3]))" would yield 1.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the interpretation of the command. There are conflicting views regarding whether the output is based on string length or the evaluation of the max function.

Contextual Notes

Participants note that the command's behavior may depend on the use of parentheses, which affects how Matlab interprets the input. There is also ambiguity regarding the distinction between string representation and numerical evaluation.

PainterGuy
Messages
938
Reaction score
73
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
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   Reactions: PainterGuy
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.
 
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   Reactions: PainterGuy
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   Reactions: PainterGuy

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K