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]
test 3 is right only 2 columns changed
Why am I getting wrong answer for this [A(:,end),(:,2:end-1),(:,1)]
Is The Test Suite 3 right?
B(:,end) = A(:,1);
B(:,1) = A(:,end);
x = A(:,1);
y = A(:,end);
function B = swap_ends(A)
Why isn´t my code working? And why isn´t B_correct =  (test 3) the right answer?
not a general solution, will fail A=[1;2]. Use size iso lengt
function B = swap_ends(A)
B = A(:,[end , 2:end-1 , 1]);
why its not working ??
@Zunaed It doesn't work because it fails the 4th test case of A = . I used that same code in the MATLAB software and it outputs B = [1 1] instead of B =  since you're printing out the last and first element of the matrix. You only should be printing  not [1 1]
Se vuoi ti faccio un paio di ripetizioni swap a 5$
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
"Undefined function or variable 'C1'."
You have made assignments in wrong order.
Your code says: A(:,1)=C1
Should say: C1 = A(:,1);
B = A;
B(:,[1 end]) = A(:,[end 1]);
Really an impressing answer!
there is problem on this exam
fliplr flips all elements in rows. The problem say only 'flip' the first one and the last one
That should work
B = A([end, 2:end-1, 1])
Why isn't this acceptable?
B = A(:,[end, 2:end-1, 1])
When A is a number,end-1=0;it will be wrong;
function [B] = swap_ends(A)
%SWAP_ENDS Solution 19: Swap the first and last columns
length not correct, use size(A,2)
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=, but is not a general solution. For example, it would not generate the correct output if A=, 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 3 for correctness is wrong
test 3 is error
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]
No, this solution does not cover size(A,2)==2.
the ultimate solution to all problems
Hmm isn't it a shortest instruction ? :/ any tips ? :)
esta mal evaluado este ejercicio
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.
test suite #3 is wrong, i think
It works perfectly on Matlab. No idea why this says it failed...
For A=1, it returns B=[1 1] because A(:,end) and A(:,1) are both 1. It should return B=1. Hope this helps.
Thanks James! I figured it out
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]);
the third case also works !!!
My MATLAB worked for Test#3:
3 2 0 5 1
This solution works just fine on my matlab..why is cody returning: "Undefined function 'cody.verifyCode' for input arguments of type 'char'."
i dont understand why this code is not working
function B = swap_ends(A)
C = A(:,1);
D = A(:,2:end-1);
E = A(:,end);
B = [E D C];
Granted I'm replying to an old post, but the problem with the solution given is when A is just a single number. A = 1;
Need to improve test 3. center cols are symmetric, so even though cols 2 and 4 get swapped, the test suite can't validate.
like the fliplr, this solution is invalid... the test suite is limited.
invalid solution, it also affects the middle columns
It passes because the test suite only tests for symmetrical matrices :/
Wrong code! Not just the first and last columns swapped!
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
The test suite does not properly test solutions to this problem. This solution would fail for
A = [ 12 4 5 7
5 1 2 4];
Invalid solution. If the array has one column, this solution generates two columns.
Convert a numerical matrix into a cell array of strings
Rotate input square matrix 90 degrees CCW without rot90
Convert given decimal number to binary number.
Pernicious Anniversary Problem
Find the alphabetic word product
Add two numbers
Find relatively common elements in matrix rows
Most nonzero elements in row
Find the treasures in MATLAB Central and discover how the community can help you!
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Contact your local office