6

Is there a one-liner in MATLAB for this?

if a > b
    foo = 'r';
else
    foo = 'g';
end
Dan
  • 45,079
  • 17
  • 88
  • 157
Sibbs Gambling
  • 19,274
  • 42
  • 103
  • 174
  • 1
    @jkalden I think SO should surely revise its "duplicity policy". The linked question has a title of zero information. No one will click into that question. Not the case here. – Sibbs Gambling Dec 19 '14 at 08:21
  • 2
    @SibbsGambling but anyone (i.e. you) can edit the duplicate title. In fact I'll do it now. – Dan Dec 19 '14 at 08:24
  • Is it about content or headlines? I found it with a search of less than a minute. Though I'm rather new to SO, i was quite often told that you should search before asking a question. You could edit the headline of the other question to improve it... – jkalden Dec 19 '14 at 08:24

3 Answers3

13

There is no syntactic sugar for one-line if-statements in MatLab, but if your statement is really simple you could write it in one line.

I used to have one-line if-statements like that in my old project:

if (k < 1); k = 1; end;

In your case it'll look something like:

if a > b; foo = 'r'; else; foo = 'g'; end;

or, if you don't like semicolons

if a > b, foo = 'r'; else, foo = 'g'; end

Not as pretty as you may have expected, though.

Leonid Beschastny
  • 50,364
  • 10
  • 118
  • 122
9

Not as elegant as a C style ternary operator but you can take advantage of the fact that matlab will automatically cast logicals into doubles in this situation. So you can just multiply your desired result for true (r in this case) by your condition (a > b), and add that to the product of your desired result for false (i.e. g) with the not of your condition:

foo = (a > b)*c + (~(a > b))*d

so if we let c = 'r' and d = 'g' then all we need to do is cast foo back to a char at the end:

char(foo)

or

char((a > b)*'r' + ~(a > b)*'g')

Note that this will only work if c and d have the same dimensions (because of the +)...

Dan
  • 45,079
  • 17
  • 88
  • 157
  • Sorry for misleading, but I meant a broader case (see updated question). – Sibbs Gambling Dec 19 '14 at 08:23
  • @SibbsGambling this should still work in the broader case - you might just have to do a bit of casting – Dan Dec 19 '14 at 08:24
  • 1
    @SibbsGambling I still think you should question the readability of this approach and consider rather sticking with the `if` approach... – Dan Dec 24 '14 at 05:38
6

Try to avoid using if statements in matlab, and just convert your logic to (vector) math:

foo = 1 + (a <= b)

Edit:

For the more general case, of assigning 'r' or 'g', you can use:

col = {'r', 'g'};
foo = col(1 + (a > b));

So for example with an isGreen boolean you could do:

foo = col(1 + isGreen);

This could also be a boolean returning function

foo = col(1 + isGreen(a))
pashute
  • 3,965
  • 3
  • 38
  • 65
Ofri Raviv
  • 24,375
  • 3
  • 55
  • 55
  • Sorry for misleading, but I meant a broader case (see updated question). – Sibbs Gambling Dec 19 '14 at 08:23
  • @SibbsGambling - Editted after your question update. – Ofri Raviv Dec 19 '14 at 08:31
  • 5
    best answer, very general - however I would start to question the readability of the code. It probably makes more sense to stick with the `if` statements in this case so that future coders (including you) don't have to decipher what's going on. – Dan Dec 19 '14 at 08:33
  • @Dan I disagree about readability. Just give (a>b) some meaningful name (which obviously I can't because this is a contextless question), and assign it in a line of its own, then col(1 + isGreen) for example is very clear, because the "1+..." bit is just a conversion from 0-based boolean logic to 1-based matlab indices, which is obvious and familiar to anyone working in matlab. – Ofri Raviv Dec 19 '14 at 08:47