- #1

Atlas3

Gold Member

- 69

- 3

for example

for x ==3 return 1, for all other x return x.

Thanks

You are using an out of date browser. It may not display this or other websites correctly.

You should upgrade or use an alternative browser.

You should upgrade or use an alternative browser.

- MATLAB
- Thread starter Atlas3
- Start date

- #1

Atlas3

Gold Member

- 69

- 3

for example

for x ==3 return 1, for all other x return x.

Thanks

- #2

Mark44

Mentor

- 35,052

- 6,791

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.

- #3

Atlas3

Gold Member

- 69

- 3

Thank you

- #4

- 1,754

- 1,574

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:

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.

Works correctly with whitespace removed.

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

Other than that, the result is coming fine.

- #5

Atlas3

Gold Member

- 69

- 3

I appreciate your reply. Thank you. I have had the error and tried permutations trying to figure out wth. Thanks.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.

- #6

Mark44

Mentor

- 35,052

- 6,791

So, Wrichik, is this what works?

Matlab:

`f = @(x)(x == 3) * 1 + (x ~= 3) * x`

- #7

DrGreg

Science Advisor

Gold Member

- 2,328

- 1,137

Matlab:

`f = @(x) (x == 3)*1 + (x ~= 3) * x ;`

- #8

Atlas3

Gold Member

- 69

- 3

how do I mark a question answered??

Thanks

Thanks

- #9

- 1,754

- 1,574

Nope, the space is still giving an error:So, Wrichik, is this what works?

Matlab:`f = @(x)(x == 3) * 1 + (x ~= 3) * x`

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

- #10

Mark44

Mentor

- 35,052

- 6,791

- #11

- 1,754

- 1,574

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):

- #12

DrGreg

Science Advisor

Gold Member

- 2,328

- 1,137

- #13

Atlas3

Gold Member

- 69

- 3

could it be that the multiplication is attempting to be applied to a logic result.

- #14

Mark44

Mentor

- 35,052

- 6,791

That would be worth investigating.

I doubt this very much. In matlab, true has the value 1 and false has the value 0.could it be that the multiplication is attempting to be applied to a logic result.

- #15

- 1,754

- 1,574

I directly copied from here and pasted there. Should that cause any problem?That would be worth investigating.

- #16

DrClaude

Mentor

- 7,603

- 3,999

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.I directly copied from here and pasted there. Should that cause any problem?

- #17

- 1,754

- 1,574

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?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.

- #18

Mark44

Mentor

- 35,052

- 6,791

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

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:

- #19

DrGreg

Science Advisor

Gold Member

- 2,328

- 1,137

Share: