Problem 11. Back and Forth Rows
Solution Stats
Problem Comments

16 Comments
nice one
good one
i got same solution on matlab, but i don't know why i didn't here
I am no expert, but does anyone else think that the first test case is flawed? "%%n = 4;" instead of " %% (new line) n = 4;" So, "n" is never defined for the first case. Am I right? If not, I apologize!
that seems legit
nice one
Really enjoyed this one.
Can someone help me? My solution works on my matlab but gives me an error during the test! What am I supposed to do ? :(
I think the answer to test suite 5 should be c = 2 since the first row is 0.21 and the second is 0.26. Please correct me if I'm wrong.
Awesome!
Awesome
Great
Check this out.
function b = back_and_forth(n)
b=1:n^2;
b=reshape(b,n,n)';
for i=2:2:n
b(i,:)=fliplr(b(i,:));
end
end
It appears a '\r' is needed after the %% in the first test of the solution to make it process properly.
The problem with the first test case has been fixed here.
This code
b=vec2mat(1:n^2,n)
b(2:2:end,:)=fliplr(b(2:2:end,:))
work in Matlab, why it doesn't works on site?
Solution Comments

1 Comment
What is wrong with this code please help me!
function c=back_and_forth(n)
y=n.^2;%expand the number x to x.^2 value because there must be x by x matrix
g=1:y;%expand the value 1 to y
f=reshape(g,n,n);%reshape g into x by x matrix
c=f';
end

1 Comment
Check this out.
function b = back_and_forth(n)
b=1:n^2;
b=reshape(b,n,n)';
for i=2:2:n
b(i,:)=fliplr(b(i,:));
end
end

1 Comment
my code works on my PC but not on this..
a = [1:(n^2)];
a = reshape(a,[],n)'

2 Comments
How can u do it in 10
I also want to know.

1 Comment
I can't write shorter program.
(^.^;)

1 Comment
very easy

1 Comment
gj

2 Comments
Why is this not working please?
a = ones(n,n);
for i = 1:n^2;
a(i) = i;
end
a = a'
I've tried this in school and it worked fine... :O Could you please help and explain? :)
Look closer at the requirements. Even rows should be flipped, such that their values are in descending order when reading from lefttoright.

1 Comment
Why is this problem showing errors in line 6 when I have a code of 4 line?

2 Comments
It works on matlab
i got same solution on matlab, but i don't know why i didn't here

1 Comment
good Question

1 Comment
really clever :)

4 Comments
Sorry for the nasty solution. I'm hoping the use of str2num and anonymous functions will eventually be limited, since most simple problems can be solved with this cheat.
Hi,
Could you comment how using str2num and anonymous functions decreases the score from 46 to 13? Is it a glinch in the notation system?
Regards
Hi, Florent.
The scoring in Cody is based on the number of nodes in the parse tree, and strings only require one node.
You can have a look for yourself by typing:
'>> mtree('back_and_forth.m','file').show'.
Try to do it for the same code, without str2num, and you'll see that even small things like parenthesis or plus signs require their own node in the tree.
In terms of performance, the two versions are not so different. The problem here is that every solution, that use str2num in this way, will have the same Cody score, regardless of how elegant or efficient the anonymous function is.
I really like how you've split the matrix into two. That's a clever insight! And I am really interested in knowing WHY str2num requires only one node.

2 Comments
brilliant!
clever!
abs(sort(cumprod(ones(n)).*reshape(1:n^2,n,n)',2));
This one is more clear.what do you think?

1 Comment
well done,why can't come up with this method

1 Comment
Sorry, I just applied a slightly involved ans trick to the previous best solution. I hurts me though that the best solution involves a for loop.

1 Comment
This should work

2 Comments
This is faster and nicer than calling an external function to flip the direction.
Excellent
Problem Recent Solvers6405
Suggested Problems

13268 Solvers

454 Solvers

324 Solvers

547 Solvers

4813 Solvers
More from this Author95
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!