Problem 19. Swap the first and last columns
Solution Stats
Problem Comments
-
16 Comments
need to fix the test suite
Updated the test so that fliplr will fail. Rescoring. Thanks for all the comments.
Check your test case. Test case with single dimension array is wrong
it can be done in many ways
Swapping the first and last columns means the input argument must have columns more than 1.
Thus, the solution, B=1 for A=1, cannot be accepted unless it is clearly written in the problem.
Test 3 B_correct is wrong: where, test B_correct=[3 5 0 2 1] it should be B_correct=[3 2 0 5 1]
nice one!
nice one!
test 3 is right only 2 columns changed
nice
Why am I getting wrong answer for this [A(:,end),(:,2:end-1),(:,1)]
nice
very nicee
nice
good problem
Good problem.
Solution Comments
-
1 Comment
size is 10 ??????
how to finish it?
-
1 Comment
Not too shabby
-
4 Comments
good problem
GOOD
GOOD
Clever solution!
-
1 Comment
good problem
-
1 Comment
Nice problem.
-
1 Comment
Is The Test Suite 3 right?
-
2 Comments
B=A
B(:,end) = A(:,1);
B(:,1) = A(:,end);
nice
-
1 Comment
x = A(:,1);
y = A(:,end);
A(:,1)=y;
A(:,end)=x;
B=A
-
1 Comment
function B = swap_ends(A)
B=A;
[r,c]=size(A);
B(:,1)=A(:,c);
B(:,c)=A(:,1);
end
-
1 Comment
Why isn´t my code working? And why isn´t B_correct = [32051] (test 3) the right answer?
-
2 Comments
niceee
not a general solution, will fail A=[1;2]. Use size iso lengt
-
2 Comments
function B = swap_ends(A)
B = A(:,[end , 2:end-1 , 1]);
end
why its not working ??
@Zunaed It doesn't work because it fails the 4th test case of A = [1]. I used that same code in the MATLAB software and it outputs B = [1 1] instead of B = [1] since you're printing out the last and first element of the matrix. You only should be printing [1] not [1 1]
-
1 Comment
Se vuoi ti faccio un paio di ripetizioni swap a 5$
-
4 Comments
Please Help I have already run this code in matlab but still get wrong here
If Anybody can help me i strongly grateful to him
Could fail the last test, if A has only 1 column:
c = 1
Bad indexing (r,c-1) == (r,0)
You got to catch case c = 1 and don't swap at all
And................ obviosly
"Undefined function or variable 'C1'."
You have made assignments in wrong order.
Your code says: A(:,1)=C1
Should say: C1 = A(:,1);
-
3 Comments
B = A;
B(:,[1 end]) = A(:,[end 1]);
Really an impressing answer!
A good one
-
2 Comments
there is problem on this exam
fliplr flips all elements in rows. The problem say only 'flip' the first one and the last one
-
1 Comment
That should work
B = A([end, 2:end-1, 1])
-
2 Comments
Why isn't this acceptable?
B = A(:,[end, 2:end-1, 1])
When A is a number,end-1=0;it will be wrong;
-
3 Comments
function [B] = swap_ends(A)
%SWAP_ENDS Solution 19: Swap the first and last columns
if length(A)>1
B=cat(2,A(:,length(A)),A(:,2:length(A)-1),A(:,1));
else
B=A;
end
end
nice
length not correct, use size(A,2)
-
1 Comment
.
-
1 Comment
B=permute(A,4:1) ?
-
1 Comment
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".
-
1 Comment
test 3 for correctness is wrong
-
1 Comment
test 3 is error
-
1 Comment
please correct the test suite.
Test 3 B_correct is wrong:
where you test B_correct=[3 5 0 2 1]
it should be B_correct=[3 2 0 5 1]
-
1 Comment
No, this solution does not cover size(A,2)==2.
-
2 Comments
-
1 Comment
Hmm isn't it a shortest instruction ? :/ any tips ? :)
-
1 Comment
esta mal evaluado este ejercicio
-
2 Comments
Normally, after writing an A matrix, as I write like A(:,1)=A(:,end); , it is giving the actual result. Where is the mistake ?
You need to change both ends.
-
1 Comment
test suite #3 is wrong, i think
-
3 Comments
-
2 Comments
In the problem description a valid solution should also work for a 1-column matrix with more than one 1 row, like [4; 2]: "If the input has one column, the output should be identical to the input."
But solution 170924 submitted on 30 Nov 2012 (Size: 23, the leading solution) does not work in this case.
I wonder why such a case is not evaluated by the test suite.
Most probably not considered by the author while submitting the problem.
However, a test case with single column array has been added and solutions have been rescored.
-
1 Comment
In this function you need only to OVERWRITE the first and last columns, not regenerate the whole thing. for example: A(:,[1 end]) = A(:,[end 1]);
-
1 Comment
the third case also works !!!
-
1 Comment
My MATLAB worked for Test#3:
>> swap_ends(A)
ans =
3 2 0 5 1
Strange??
-
1 Comment
This solution works just fine on my matlab..why is cody returning: "Undefined function 'cody.verifyCode' for input arguments of type 'char'."
-
2 Comments
-
1 Comment
Need to improve test 3. center cols are symmetric, so even though cols 2 and 4 get swapped, the test suite can't validate.
-
1 Comment
like the fliplr, this solution is invalid... the test suite is limited.
-
2 Comments
-
3 Comments
Incorrect solution. If the array has only one column, this solution generates two columns.
Actually this solution works just fine. Try it in MATLAB. It doesn't create two columns.
You can't just do B = A(:,[end,1])...this would end up two columns if A has only one column. You should do A(:,[1,end]) = A(:,[end,1])) and then B = A
-
2 Comments
-
1 Comment
Invalid solution. If the array has one column, this solution generates two columns.
-
1 Comment
pro men =)))
Problem Recent Solvers17180
Suggested Problems
-
1355 Solvers
-
1151 Solvers
-
Longest run of consecutive numbers
4273 Solvers
-
Find last zero for each column
401 Solvers
-
615 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!