A couple of comments:
(1) The first case of setting B=1 when there is only one column in A will satisfy the test case on Cody, for which A=[1], but is not a general solution. For example, it would not generate the correct output if A=[7], nor if A=[1; 1].
(2) You have six separate, but identical, evaluations of size(A,2)). It would seem to make sense to evaluate this once only, and to assign it to a variable.
(3) Although your structure of three separate "if" statements clearly works, you could consider using instead "elseif" and "else" within a single "if" statement. Alternatively you could also use "switch, case, otherwise".
Test  Status  Code Input and Output 

1  Pass 
A = [ 12 4 7
5 1 4];
B_correct = [ 7 4 12
4 1 5 ];
assert(isequal(swap_ends(A),B_correct))
c =
12
5
d =
7
4
e =
4 7
1 4
e =
4
1
A =
12 4 7
5 1 4
B =
7 4 12
4 1 5

2  Pass 
A = [ 12 7
5 4];
B_correct = [ 7 12
4 5 ];
assert(isequal(swap_ends(A),B_correct))
c =
12
5
d =
7
4
B =
7 12
4 5

3  Pass 
A = [ 1 5 0 2 3 ];
B_correct = [ 3 5 0 2 1 ];
assert(isequal(swap_ends(A),B_correct))
c =
1
d =
3
e =
5 0 2 3
e =
5 0 2
A =
1 5 0 2 3
B =
3 5 0 2 1

4  Pass 
A = 1;
B_correct = 1;
assert(isequal(swap_ends(A),B_correct))

Make the vector [1 2 3 4 5 6 7 8 9 10]
33954 Solvers
Find the maximum number of decimal places in a set of numbers
430 Solvers
635 Solvers
241 Solvers
439 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!