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

  • MATLAB
  • Thread starter PainterGuy
  • Start date
  • #1
PainterGuy
934
68
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?
 

Answers and Replies

  • #2
Wrichik Basu
Insights Author
Gold Member
2022 Award
2,031
2,274
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
 
  • #3
PainterGuy
934
68
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
Wrichik Basu
Insights Author
Gold Member
2022 Award
2,031
2,274
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.
 
  • #5
Image Analyst
32
10
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.
 

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

  • Last Post
Replies
4
Views
1K
  • Last Post
Replies
6
Views
874
  • Last Post
Replies
0
Views
511
  • Last Post
Replies
2
Views
810
Replies
8
Views
1K
  • Last Post
Replies
1
Views
407
  • Last Post
Replies
2
Views
927
Replies
5
Views
818
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
1
Views
909
Top