Cody

# Problem 44243. Ternary Conditional Operator

Solution 2980732

Submitted on 22 Sep 2020
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
mkdir @function_handle movefile colon1.m @function_handle/colon.m

2   Fail
assert(isequal((2 > 1) : (@() 1) : (@() 2), 1))

Unrecognized function or variable 'exp1'. Error in : (line 2) y = test:exp1:exp2 Error in Test2 (line 1) assert(isequal((2 > 1) : (@() 1) : (@() 2), 1))

3   Fail
assert(isequal((1 > 2) : (@() 1) : (@() 2), 2))

Unrecognized function or variable 'exp1'. Error in : (line 2) y = test:exp1:exp2 Error in Test3 (line 1) assert(isequal((1 > 2) : (@() 1) : (@() 2), 2))

4   Fail
fib = @(f, n) (n > 2) : (@() f(f, n - 1) + f(f, n - 2)) : (@() 1); assert(fib(fib, 20) == 6765)

Unrecognized function or variable 'exp1'. Error in : (line 2) y = test:exp1:exp2 Error in Test4>@(f,n)(n>2):(@()f(f,n-1)+f(f,n-2)):(@()1) (line 1) fib = @(f, n) (n > 2) : (@() f(f, n - 1) + f(f, n - 2)) : (@() 1); Error in Test4 (line 2) assert(fib(fib, 20) == 6765)

5   Fail
x = magic(3); [m,I] = (x(1) > 0) : (@() max(x)) : (@() min(x)) assert(isequal(m, [8 9 7]) && isequal(I, [1 3 2]))

Error using : Too many output arguments. Error in Test5 (line 2) [m,I] = (x(1) > 0) : (@() max(x)) : (@() min(x))

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!