Need help defining an anonymous function in Matlab

  • Context: MATLAB 
  • Thread starter Thread starter Atlas3
  • Start date Start date
  • Tags Tags
    Function Matlab
Click For Summary

Discussion Overview

The discussion revolves around defining a step function as an anonymous function in MATLAB. Participants explore the syntax and potential issues related to whitespace and character encoding when implementing this function.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant asks how to define a step function in MATLAB that returns 1 for x equal to 3 and returns x for all other values.
  • Another participant proposes a specific anonymous function syntax but notes they cannot test it due to not having MATLAB.
  • Some participants express concerns about whitespace causing errors in the function definition.
  • There is a suggestion that the "x=" part of the function definition is incorrect and should be omitted.
  • One participant speculates that the issue with whitespace might be due to invisible characters or formatting differences when copying and pasting code.
  • Another participant questions whether the multiplication in the function is being applied to a logical result and expresses doubt about this affecting the outcome.
  • There are discussions about the potential for non-printing characters to cause issues when copying code from different sources.

Areas of Agreement / Disagreement

Participants express varying opinions on the correct syntax for the anonymous function and the impact of whitespace and character encoding on its execution. No consensus is reached regarding the exact cause of the errors encountered.

Contextual Notes

Participants mention specific errors related to whitespace and the "x=" syntax, but the exact nature of these issues remains unresolved. There are also references to differences in how text is formatted across different platforms.

Atlas3
Gold Member
Messages
69
Reaction score
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
 
Physics news on Phys.org
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.
 
Thank you
 
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 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:

Screenshot_20181203-234538.jpg


Works correctly with whitespace removed.

Another thing: the "x=" part is also giving an error:

Screenshot_20181203-234859.jpg


Other than that, the result is coming fine.
 

Attachments

  • Screenshot_20181203-234538.jpg
    Screenshot_20181203-234538.jpg
    27.3 KB · Views: 1,184
  • Screenshot_20181203-234859.jpg
    Screenshot_20181203-234859.jpg
    26.1 KB · Views: 984
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.
I appreciate your reply. Thank you. I have had the error and tried permutations trying to figure out wth. Thanks.
 
  • Like
Likes   Reactions: Wrichik Basu
So, Wrichik, is this what works?
Matlab:
f = @(x)(x == 3) * 1 + (x ~= 3) * x
 
how do I mark a question answered??
Thanks
 
Mark44 said:
So, Wrichik, is this what works?
Matlab:
f = @(x)(x == 3) * 1 + (x ~= 3) * x
Nope, the space is still giving an error:

Screenshot_20181204-011331.jpg


Could be a bug in the app. I don't have Matlab desktop version, so I cannot confirm.
 

Attachments

  • Screenshot_20181204-011331.jpg
    Screenshot_20181204-011331.jpg
    37.5 KB · Views: 650
  • #10
Well, that sucks... There aren't many programming languages that throw an error because you have included a space.
 
  • #11
Mark44 said:
Well, that sucks... There aren't many programming languages that throw an error because you have included a space.
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):

Screenshot_20181204-012155.png
 

Attachments

  • Screenshot_20181204-012155.png
    Screenshot_20181204-012155.png
    20.6 KB · Views: 543
  • #12
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?
 
  • #13
could it be that the multiplication is attempting to be applied to a logic result.
 
  • #14
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?
That would be worth investigating.

Atlas3 said:
could it be that the multiplication is attempting to be applied to a logic result.
I doubt this very much. In matlab, true has the value 1 and false has the value 0.
 
  • #15
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?
Mark44 said:
That would be worth investigating.
I directly copied from here and pasted there. Should that cause any problem?
 
  • #16
Wrichik Basu said:
I directly copied from here and pasted there. Should that cause any problem?

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.
 
  • #17
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.
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?
 
  • #18
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?
The two web sites might be formatting text differently, with one using non-printing characters like &nbsp (non-breaking space) or similar.

I'm going to guess that my 2nd stab at it will actually work, with the extra spaces added for readability not affecting anything.
Matlab:
f = @(x) (x == 3) * 1 + (x ~= 3) * x
 
Last edited:
  • Like
Likes   Reactions: Wrichik Basu
  • #19
One more thought: the text inside a "Code (Matlab M)" box on this forum is coloured, which means there is colour information as well as text. When you copy and paste into Matlab, the colour information may be corrupting the text (Rich Text?).
 
  • Like
Likes   Reactions: Wrichik Basu

Similar threads

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