Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Commonmark migration
Source Link

#MATLAB

MATLAB

In MATLAB, the plus function can be called in two different ways, either by using +, or writing plus. If you try to add values of different types, for instance a cell with an integer, you'll get an error stating what the error is, for instance:

plus(3,{3})
Undefined function 'plus' for input arguments of type 'cell'.
plus(2,2,2)
Error using + 
Too many input arguments.

This of course, is helpful, as it tell's you what the problem is: You cannot add a cell and an integer, and for some reason, you can't add three integers either. Typing help plus won't help you a lot, it just tells you that the plus-function can add two arrays.

The best approach seems to be to create a very simple plus-function that is both simple to read, and simple to understand.

plus=@(x,y) x + y + logical((x==(isnumeric(x)+isfinite(x)) & y==(isnumeric(y)+isfinite(y))));

This function checks if both arguments, x and y are numeric and finite. If not, it will fail, just as the original plus-function. The advantage is, you still get to know what the error is, but you also get too know where, and why!

plus(3,3)
ans =
 6
plus(3,{2})
Undefined function 'plus' for input arguments of type 'cell'.
Error in @(x,y)x+y+logical((x==(isnumeric(x)+isreal(x))&y==(isnumeric(y)+isreal(y))))

And of course:

plus(2,2)
ans =
 5

x==(isnumeric(x)+isreal(x)) returns 1 if x = 2, because the two functions isnumeric and isreal will both return 1, thus 1+1. The same goes with y==(isnumeric(y)+isreal(y)) of course. Now, we check if both those conditions are true, and return a logical 0 or 1: logical((x==(isnumeric(x)+isreal(x))&y==(isnumeric(y)+isreal(y)))). The logical function is strictly not necessary, but I think it makes it a bit harder to spot (for an untrained eye of course). In MATLAB, boolean values can be added to integers, so for x & y == 2, this will return 2+2=5. For any other values, it will return the correct answer.

#MATLAB

In MATLAB, the plus function can be called in two different ways, either by using +, or writing plus. If you try to add values of different types, for instance a cell with an integer, you'll get an error stating what the error is, for instance:

plus(3,{3})
Undefined function 'plus' for input arguments of type 'cell'.
plus(2,2,2)
Error using + 
Too many input arguments.

This of course, is helpful, as it tell's you what the problem is: You cannot add a cell and an integer, and for some reason, you can't add three integers either. Typing help plus won't help you a lot, it just tells you that the plus-function can add two arrays.

The best approach seems to be to create a very simple plus-function that is both simple to read, and simple to understand.

plus=@(x,y) x + y + logical((x==(isnumeric(x)+isfinite(x)) & y==(isnumeric(y)+isfinite(y))));

This function checks if both arguments, x and y are numeric and finite. If not, it will fail, just as the original plus-function. The advantage is, you still get to know what the error is, but you also get too know where, and why!

plus(3,3)
ans =
 6
plus(3,{2})
Undefined function 'plus' for input arguments of type 'cell'.
Error in @(x,y)x+y+logical((x==(isnumeric(x)+isreal(x))&y==(isnumeric(y)+isreal(y))))

And of course:

plus(2,2)
ans =
 5

x==(isnumeric(x)+isreal(x)) returns 1 if x = 2, because the two functions isnumeric and isreal will both return 1, thus 1+1. The same goes with y==(isnumeric(y)+isreal(y)) of course. Now, we check if both those conditions are true, and return a logical 0 or 1: logical((x==(isnumeric(x)+isreal(x))&y==(isnumeric(y)+isreal(y)))). The logical function is strictly not necessary, but I think it makes it a bit harder to spot (for an untrained eye of course). In MATLAB, boolean values can be added to integers, so for x & y == 2, this will return 2+2=5. For any other values, it will return the correct answer.

MATLAB

In MATLAB, the plus function can be called in two different ways, either by using +, or writing plus. If you try to add values of different types, for instance a cell with an integer, you'll get an error stating what the error is, for instance:

plus(3,{3})
Undefined function 'plus' for input arguments of type 'cell'.
plus(2,2,2)
Error using + 
Too many input arguments.

This of course, is helpful, as it tell's you what the problem is: You cannot add a cell and an integer, and for some reason, you can't add three integers either. Typing help plus won't help you a lot, it just tells you that the plus-function can add two arrays.

The best approach seems to be to create a very simple plus-function that is both simple to read, and simple to understand.

plus=@(x,y) x + y + logical((x==(isnumeric(x)+isfinite(x)) & y==(isnumeric(y)+isfinite(y))));

This function checks if both arguments, x and y are numeric and finite. If not, it will fail, just as the original plus-function. The advantage is, you still get to know what the error is, but you also get too know where, and why!

plus(3,3)
ans =
 6
plus(3,{2})
Undefined function 'plus' for input arguments of type 'cell'.
Error in @(x,y)x+y+logical((x==(isnumeric(x)+isreal(x))&y==(isnumeric(y)+isreal(y))))

And of course:

plus(2,2)
ans =
 5

x==(isnumeric(x)+isreal(x)) returns 1 if x = 2, because the two functions isnumeric and isreal will both return 1, thus 1+1. The same goes with y==(isnumeric(y)+isreal(y)) of course. Now, we check if both those conditions are true, and return a logical 0 or 1: logical((x==(isnumeric(x)+isreal(x))&y==(isnumeric(y)+isreal(y)))). The logical function is strictly not necessary, but I think it makes it a bit harder to spot (for an untrained eye of course). In MATLAB, boolean values can be added to integers, so for x & y == 2, this will return 2+2=5. For any other values, it will return the correct answer.

deleted 126 characters in body
Source Link
Stewie Griffin
  • 46.8k
  • 16
  • 137
  • 304

#MATLAB

In MATLAB, the plus function can be called in two different ways, either by using +, or writing plus. If you try to add values of different types, for instance a cell with an integer, you'll get an error stating what the error is, for instance:

plus(3,{3})
Undefined function 'plus' for input arguments of type 'cell'.
plus(2,2,2)
Error using + 
Too many input arguments.

This of course, is helpful, as it tell's you what the problem is: You cannot add a cell and an integer, and for some reason, you can't add three integers either. Typing help plus won't help you a lot, it just tells you that the plus-function can add two arrays.

The best approach seems to be to create a very simple plus-function that is both simple to read, and simple to understand.

plus=@(x,y) x + y + logical((x==(isnumeric(x)+isfinite(x)) & y==(isnumeric(y)+isfinite(y))));

This function checks if both arguments, x and y are numeric and finite. If not, it will fail, just as the original plus-function. The advantage is, you still get to know what the error is, but you also get too know where, and why!

plus(3,3)
ans =
 6
plus(3,{2})
Undefined function 'plus' for input arguments of type 'cell'.
Error in @(x,y)x+y+logical((x==(isnumeric(x)+isreal(x))&y==(isnumeric(y)+isreal(y))))

And of course:

plus(2,2)
ans =
 5

x==(isnumeric(x)+isreal(x)) returns 1 if x = 2, because the two functions isnumeric and isreal will both return 1, thus 1+1. The same goes with y==(isnumeric(y)+isreal(y)) of course. Now, we check if both those conditions are true, and return a logical 0 or 1: logical((x==(isnumeric(x)+isreal(x))&y==(isnumeric(y)+isreal(y)))). The logical function is strictly not necessary, but I think it makes it a bit harder to spot (for an untrained eye of course). In MATLAB, boolean values can be added to integers, so for x & y == 2, this will return 2+2=5. For any other values, it will return the correct answer. I don't expect this to fool many MATLAB-users, but it might fool someone that's not used to how MATLAB treats logical values.

#MATLAB

In MATLAB, the plus function can be called in two different ways, either by using +, or writing plus. If you try to add values of different types, for instance a cell with an integer, you'll get an error stating what the error is, for instance:

plus(3,{3})
Undefined function 'plus' for input arguments of type 'cell'.
plus(2,2,2)
Error using + 
Too many input arguments.

This of course, is helpful, as it tell's you what the problem is: You cannot add a cell and an integer, and for some reason, you can't add three integers either. Typing help plus won't help you a lot, it just tells you that the plus-function can add two arrays.

The best approach seems to be to create a very simple plus-function that is both simple to read, and simple to understand.

plus=@(x,y) x + y + logical((x==(isnumeric(x)+isfinite(x)) & y==(isnumeric(y)+isfinite(y))));

This function checks if both arguments, x and y are numeric and finite. If not, it will fail, just as the original plus-function. The advantage is, you still get to know what the error is, but you also get too know where, and why!

plus(3,3)
ans =
 6
plus(3,{2})
Undefined function 'plus' for input arguments of type 'cell'.
Error in @(x,y)x+y+logical((x==(isnumeric(x)+isreal(x))&y==(isnumeric(y)+isreal(y))))

And of course:

plus(2,2)
ans =
 5

x==(isnumeric(x)+isreal(x)) returns 1 if x = 2, because the two functions isnumeric and isreal will both return 1, thus 1+1. The same goes with y==(isnumeric(y)+isreal(y)) of course. Now, we check if both those conditions are true, and return a logical 0 or 1: logical((x==(isnumeric(x)+isreal(x))&y==(isnumeric(y)+isreal(y)))). The logical function is strictly not necessary, but I think it makes it a bit harder to spot (for an untrained eye of course). In MATLAB, boolean values can be added to integers, so for x & y == 2, this will return 2+2=5. For any other values, it will return the correct answer. I don't expect this to fool many MATLAB-users, but it might fool someone that's not used to how MATLAB treats logical values.

#MATLAB

In MATLAB, the plus function can be called in two different ways, either by using +, or writing plus. If you try to add values of different types, for instance a cell with an integer, you'll get an error stating what the error is, for instance:

plus(3,{3})
Undefined function 'plus' for input arguments of type 'cell'.
plus(2,2,2)
Error using + 
Too many input arguments.

This of course, is helpful, as it tell's you what the problem is: You cannot add a cell and an integer, and for some reason, you can't add three integers either. Typing help plus won't help you a lot, it just tells you that the plus-function can add two arrays.

The best approach seems to be to create a very simple plus-function that is both simple to read, and simple to understand.

plus=@(x,y) x + y + logical((x==(isnumeric(x)+isfinite(x)) & y==(isnumeric(y)+isfinite(y))));

This function checks if both arguments, x and y are numeric and finite. If not, it will fail, just as the original plus-function. The advantage is, you still get to know what the error is, but you also get too know where, and why!

plus(3,3)
ans =
 6
plus(3,{2})
Undefined function 'plus' for input arguments of type 'cell'.
Error in @(x,y)x+y+logical((x==(isnumeric(x)+isreal(x))&y==(isnumeric(y)+isreal(y))))

And of course:

plus(2,2)
ans =
 5

x==(isnumeric(x)+isreal(x)) returns 1 if x = 2, because the two functions isnumeric and isreal will both return 1, thus 1+1. The same goes with y==(isnumeric(y)+isreal(y)) of course. Now, we check if both those conditions are true, and return a logical 0 or 1: logical((x==(isnumeric(x)+isreal(x))&y==(isnumeric(y)+isreal(y)))). The logical function is strictly not necessary, but I think it makes it a bit harder to spot (for an untrained eye of course). In MATLAB, boolean values can be added to integers, so for x & y == 2, this will return 2+2=5. For any other values, it will return the correct answer.

added 58 characters in body
Source Link
Stewie Griffin
  • 46.8k
  • 16
  • 137
  • 304

#MATLAB

In MATLAB, the plus function can be called in two different ways, either by using +, or writing plus. If you try to add values of different types, for instance a cell with an integer, you'll get an error stating what the error is, for instance:

plus(3,{3})
Undefined function 'plus' for input arguments of type 'cell'.
plus(2,2,2)
Error using + 
Too many input arguments.

This of course, is helpful, as it tell's you what the problem is: You cannot add a cell and an integer, and for some reason, you can't add three integers either. Typing help plus won't help you a lot, it just tells you that the plus-function can add two arrays.

The best approach seems to be to create a very simple plus-function that is both simple to read, and simple to understand.

plus=@(x,y) x + y + logical((x==(isnumeric(x)+isfinite(x)) & y==(isnumeric(y)+isfinite(y))));

This function checks if both arguments, x and y are numeric and finite. If not, it will fail, just as the original plus-function. The advantage is, you still get to know what the error is, but you also get too know where, and why!

plus(3,3)
ans =
 6
plus(3,{2})
Undefined function 'plus' for input arguments of type 'cell'.
Error in @(x,y)x+y+logical((x==(isnumeric(x)+isreal(x))&y==(isnumeric(y)+isreal(y))))

And of course:

plus(2,2)
ans =
 5

x==(isnumeric(x)+isreal(x)) returns 1 if x = 2, because the two functions isnumeric and isreal will both return 1, thus 1+1. The same goes with y==(isnumeric(y)+isreal(y)) of course. Now, we check if both those conditions are true, and return a logical 0 or 1: logical((x==(isnumeric(x)+isreal(x))&y==(isnumeric(y)+isreal(y)))). The logical function is strictly not necessary, but I think it makes it a bit harder to spot (for an untrained eye of course). In MATLAB, boolean values can be added to integers, so for x & y == 2, this will return 2+2=5. For any other values, it will return the correct answer. I don't expect this to fool many MATLAB-users, but it might fool someone that's not used to how MATLAB treats logical values.

#MATLAB

In MATLAB, the plus function can be called in two different ways, either by using +, or writing plus. If you try to add values of different types, for instance a cell with an integer, you'll get an error stating what the error is, for instance:

plus(3,{3})
Undefined function 'plus' for input arguments of type 'cell'.
plus(2,2,2)
Error using + 
Too many input arguments.

This of course, is helpful, as it tell's you what the problem is: You cannot add a cell and an integer, and for some reason, you can't add three integers either. Typing help plus won't help you a lot, it just tells you that the plus-function can add two arrays.

The best approach seems to be to create a very simple plus-function that is both simple to read, and simple to understand.

plus=@(x,y) x + y + logical((x==(isnumeric(x)+isfinite(x)) & y==(isnumeric(y)+isfinite(y))));

This function checks if both arguments, x and y are numeric and finite. If not, it will fail, just as the original plus-function. The advantage is, you still get to know what the error is, but you also get too know where, and why!

plus(3,3)
ans =
 6
plus(3,{2})
Undefined function 'plus' for input arguments of type 'cell'.
Error in @(x,y)x+y+logical((x==(isnumeric(x)+isreal(x))&y==(isnumeric(y)+isreal(y))))

And of course:

plus(2,2)
ans =
 5

#MATLAB

In MATLAB, the plus function can be called in two different ways, either by using +, or writing plus. If you try to add values of different types, for instance a cell with an integer, you'll get an error stating what the error is, for instance:

plus(3,{3})
Undefined function 'plus' for input arguments of type 'cell'.
plus(2,2,2)
Error using + 
Too many input arguments.

This of course, is helpful, as it tell's you what the problem is: You cannot add a cell and an integer, and for some reason, you can't add three integers either. Typing help plus won't help you a lot, it just tells you that the plus-function can add two arrays.

The best approach seems to be to create a very simple plus-function that is both simple to read, and simple to understand.

plus=@(x,y) x + y + logical((x==(isnumeric(x)+isfinite(x)) & y==(isnumeric(y)+isfinite(y))));

This function checks if both arguments, x and y are numeric and finite. If not, it will fail, just as the original plus-function. The advantage is, you still get to know what the error is, but you also get too know where, and why!

plus(3,3)
ans =
 6
plus(3,{2})
Undefined function 'plus' for input arguments of type 'cell'.
Error in @(x,y)x+y+logical((x==(isnumeric(x)+isreal(x))&y==(isnumeric(y)+isreal(y))))

And of course:

plus(2,2)
ans =
 5

x==(isnumeric(x)+isreal(x)) returns 1 if x = 2, because the two functions isnumeric and isreal will both return 1, thus 1+1. The same goes with y==(isnumeric(y)+isreal(y)) of course. Now, we check if both those conditions are true, and return a logical 0 or 1: logical((x==(isnumeric(x)+isreal(x))&y==(isnumeric(y)+isreal(y)))). The logical function is strictly not necessary, but I think it makes it a bit harder to spot (for an untrained eye of course). In MATLAB, boolean values can be added to integers, so for x & y == 2, this will return 2+2=5. For any other values, it will return the correct answer. I don't expect this to fool many MATLAB-users, but it might fool someone that's not used to how MATLAB treats logical values.

added 58 characters in body
Source Link
Stewie Griffin
  • 46.8k
  • 16
  • 137
  • 304
Loading
deleted 27 characters in body
Source Link
Stewie Griffin
  • 46.8k
  • 16
  • 137
  • 304
Loading
Post Undeleted by Stewie Griffin
added 1119 characters in body
Source Link
Stewie Griffin
  • 46.8k
  • 16
  • 137
  • 304
Loading
Post Deleted by Stewie Griffin
Source Link
Stewie Griffin
  • 46.8k
  • 16
  • 137
  • 304
Loading

AltStyle によって変換されたページ (->オリジナル) /