Flip the outermost columns of matrix A, so that the first column becomes the last and the last column becomes the first. All other columns should be left intact. Return the result in matrix B.
If the input has one column, the output should be identical to the input.
Example:
Input A = [ 12 4 7 5 1 4 ]; Output B is [ 7 4 12 4 1 5 ];
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)]
niceee
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]);
end
why its not working ??
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
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);
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
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)
.
B=permute(A,4:1) ?
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 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.
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
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:
>> swap_ends(A)
ans =
3 2 0 5 1
Strange??
This solution works just fine on my matlab..why is cody returning: "Undefined function 'cody.verifyCode' for input arguments of type 'char'."
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.
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
Invalid solution. If the array has one column, this solution generates two columns.
570 Solvers
Project Euler: Problem 6, Natural numbers, squares and sums.
794 Solvers
413 Solvers
Convert a vector into a number
445 Solvers
314 Solvers