- #1
Atlas3
Gold Member
- 69
- 3
How can a step function be defined as an anonymous function in Matlab?
for example
for x ==3 return 1, for all other x return x.
Thanks
for example
for x ==3 return 1, for all other x return x.
Thanks
f = @(x) x = (x == 3)*1 + (x ~= 3) * x
I don't know if this is common for anonymous functions, but I found that whitespace is illegal, unlike java or any other programming language. In MATLAB mobile:Mark44 said:I think this will work:
Matlab:f = @(x) x = (x == 3)*1 + (x ~= 3) * x
f(2) should evaluate to 2 and f(3) should evaluate to 1.
I don't have matlab, so I can't test this out, but according to the MathWorks documentation on anonymous functions, it should work. The code I wrote is a bit more cumbersome than equivalent code in C/C++, since MATLAB doesn't have a conditional operator.
I appreciate your reply. Thank you. I have had the error and tried permutations trying to figure out wth. Thanks.Wrichik Basu said:I don't know if this is common for anonymous functions, but I found that whitespace is illegal, unlike java or any other programming language. In MATLAB mobile:
View attachment 235158
Works correctly with whitespace removed.
Another thing: the "x=" part is also giving an error:
View attachment 235159
Other than that, the result is coming fine.
f = @(x)(x == 3) * 1 + (x ~= 3) * x
f = @(x) (x == 3)*1 + (x ~= 3) * x ;
It is quite frustrating, and I don't have an explanation. I tried all possible combinations, and this is the one (at the bottom) that was evaluated correctly (mark the spaces):Mark44 said:Well, that sucks... There aren't many programming languages that throw an error because you have included a space.
That would be worth investigating.DrGreg said:I wonder if the "invalid text character" isn't a space but some other character that looks like a space? Or an invisible character that is there as well as a space?
I doubt this very much. In matlab, true has the value 1 and false has the value 0.Atlas3 said:could it be that the multiplication is attempting to be applied to a logic result.
DrGreg said:I wonder if the "invalid text character" isn't a space but some other character that looks like a space? Or an invisible character that is there as well as a space?
I directly copied from here and pasted there. Should that cause any problem?Mark44 said:That would be worth investigating.
That is most probably the problem. Matlab has no problem with spaces (ASCII code 32), so it must be another character that is copied and pasted.Wrichik Basu said:I directly copied from here and pasted there. Should that cause any problem?
You've got it right. But I copy-paste from the MATLAB website as well, but that doesn't create a problem. How is this possible?DrClaude said:That is most probably the problem. Matlab has no problem with spaces (ASCII code 32), so it must be another character that is copied and pasted.
The two web sites might be formatting text differently, with one using non-printing characters like   (non-breaking space) or similar.Wrichik Basu said:You've got it right. But I copy-paste from the MATLAB website as well, but that doesn't create a problem. How is this possible?
f = @(x) (x == 3) * 1 + (x ~= 3) * x
An anonymous function in Matlab is a function that is defined without a name. It is also known as a "function handle" or a "lambda function". It is useful for creating short, one-line functions without the need to define a separate function file.
To define an anonymous function in Matlab, use the "@" symbol followed by the input arguments in parentheses, then the function body. For example, @(x,y) x + y will create an anonymous function that adds two input variables.
Yes, you can use anonymous functions in place of named functions in Matlab. They can be passed as inputs to other functions, used in array operations, and assigned to variables.
There are several advantages of using anonymous functions in Matlab. They are concise and can be used to create small, one-line functions without the need for a separate file. They also allow for more flexible code and can be used in many different ways, such as passing them as inputs to other functions.
Yes, you can call an anonymous function multiple times in Matlab. Once it is defined, you can call it as many times as needed with different input arguments. However, it is important to note that anonymous functions are local to the workspace in which they are defined, so they cannot be called from outside that workspace.