You are given a 2D matrix of dimension [m,n] and a positive integer r. You have to rotate the matrix r times and print the resultant matrix. Rotation should be performed in alternating layers of matrix
Example (for r=1)
Start with rotating the outer layer counter-clockwise, reverse the direction with each layer.
y = [1 2 3 4 5 6 [2 3 4 5 6 12
7 8 9 10 11 12 1 14 8 9 10 18
13 14 15 16 17 18 -> 7 20 16 22 11 24
19 20 21 22 23 24 -> 13 26 15 21 17 30
25 26 27 28 29 30 19 27 28 29 23 36
31 32 33 34 35 36] 25 31 32 33 34 35]
y = [7 8 9 10 [8 9 10 11
6 1 2 11 -> 7 4 1 12
5 4 3 12 -> 6 3 2 13
16 15 14 13] 5 16 15 14]
y = ['abcde' ['bcdej'
'fghij' -> 'amghp'
'lmnop' -> 'fnoit'
'pqrst'] 'lpqrs']
See test suite for further clarity.
Solution Stats
Problem Comments
8 Comments
Solution Comments
Show comments
Loading...
Problem Recent Solvers15
Suggested Problems
-
Find the longest sequence of 1's in a binary sequence.
6685 Solvers
-
1912 Solvers
-
Back to basics 9 - Indexed References
461 Solvers
-
Find the elements of a matrix according to a defined property.
91 Solvers
-
Test Problem; Create a 5x5 array containing all ones
405 Solvers
More from this Author44
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
There are several errors in the "correct" values for the tests. For example, text #13 has a capital P replaced with a lower case p. Test 11 seems to have an iincorrect value for n. There are also errors in tests 5, 6 and 8.
I'm getting different answers for tests 5, 6, 8, 11, and 13.
In test 6 the center point should not change; in test 8 the center 3 by 3 should not change; test 11 would be ok if n=2,6,10,....
I agree with Tim's comments on tests 6 and 8. I would sum things up by saying that the correct results for tests 6, 8, 11 and 12 are:
Problem 6:
y = [2 1 2 1 2; 1 2 1 2 1; 2 1 1 1 2; 1 2 1 2 1; 2 1 2 1 2];
Problem 8:
y = [0 0 0 0 1; 0 1 0 0 0; 0 0 1 0 0; 0 0 0 1 0; 1 0 0 0 0];
Problem 11:
y = [2 1 2 1 2 1 2; 1 4 5 4 3 4 1; 2 3 2 1 2 5 2; 1 4 1 1 1 4 1; 2 5 2 1 2 3 2; 1 4 3 4 5 4 1; 2 1 2 1 2 1 2];
Problem 12:
y = [13 14 15 16; 9 10 11 12; 5 6 7 8; 1 2 3 4];
I hate to say it, but tests 8 and 12 still seem to be incorrect.
Yes, I agree. Those two are still comparing to matrices that are different than the correct results that I included in my previous comment.
William and Tim, apologies for the fiasco. The test suite has been corrected completely.
Tim, your answer has been rescored and accepted. Do rate the question!
It works! Thanks Dyuman.