Problem 44350. Breaking Out of the Matrix
Do you want to take the Red Pill, or the Blue Pill?
If you take the Blue Pill, you will simply pass along to the next problem, not knowing what Cody has in store for you.
If you take the Red Pill, you will be asked to write a MATLAB function that will Break a Matrix. The inputs to the function will be a matrix M, along with a number of rows (R) and columns (C). You goal is to break the larger 2D matrix up into a 3D matrix comprised of enough RxC matrices so that you can recreate the 2D matrix. When creating your 3D matrix, go down the columns first, and then across the rows. Increment only one column (or one row) at a time. Do not go C columns down at each step.
For example, R=2 and C=3, and M is as follows:
M=[1 4 7 10 2 5 8 11 3 6 9 12]
This means that your output should be a 2x3x4 matrix:
X(:,:,1) = 1 4 7 2 5 8 X(:,:,2) = 2 5 8 3 6 9 X(:,:,3) = 4 7 10 5 8 11 X(:,:,4) = 5 8 11 6 9 12
You can assume that R and C will always be less than or equal to the appropriate dimension of the original matrix. Good luck!
Solution Stats
Problem Comments

14 Comments
Is the second test case correct?
I guess it should be M(:,:,y)==x(y).
Your syntax works just as well, Yuichi. With the arrayfun @(y) going from 1:numel(x), y=x(y).
Is the 3rd test case good? I think something is wrong there!
All the other cases pass but the 3rd fails with this error:
"Index exceeds matrix dimensions. Error in Test3>@(a)isequal(M(:,:,a),eye(2)) Error in Test3 (line 7) a1=arrayfun(@(a) isequal(M(:,:,a),eye(2)),ids);"
I checked the result manually and it should be fine (provided that I understood the task right)
Viko, while it's possible that my test suite has an issue (just check out my Hard problem on Pandigital Numbers for an example in that!) we've had a lot of solvers on this one. It's likely that one of them would have found something wrong with the test suite. However, it's still possible that your solution hit a weird corner case because you were doing it in a unique manner. Please post a comment from the solution that gave you that error so I can take a look at your code.
Hey, I have similar problem as Viko (the same error). I am just wondering why is the function checking submatrix up until index 36 when matrix M (derived from matrix x = eye(7)) has dimensions (2,2,16).
Viko and Martin  If you are breaking down a 7x7 matrix into multiple 2x2 matrices, you will end up with 36 of them. You will end up with 16 of them if you are breaking a 7x7 up into a set of 4x4 matrices, or if your 2x2 matrices do not overlap with each other.
Set A=reshape(1:49,7,7) Because you are breaking A up into multiple 2x2 matrices, the first matrix will be [1 8 ; 2 9] The next one will be [2 9, 3 10]. It will continue all the way down and across to your final matrix  [41 48 ; 42 49]. There will be 36 of them total. If you are getting a 2x2x16 matrix, you may have misunderstood the problem. Please post a comment from your solution so I can see your code.
Forgot to add that if your 2x2 matrices do not overlap in the 7x7, you will get an error since you will run out of space in the last series of matrices. Perhaps that is your problem?
Didn´t like that one. Sorry.
The Problem doesn´t specify if there´s any intentional overlap. I had to try ond error through the test bench. The test bench should be for testing only (not for describing the Problem), in my opinion. Or is there any intuitive or obvious way i am missing?
Anselm, the problem statement, which describes how you should break a 3x4 matrix up into 2x3 matrices, shows that there are common values in the matrices that comprise the correct solution. The bottom row for X(:,:,1) [2 5 8] is the top row of X(:,:,2). Likewise, the last two columns of X(:,:,1) [4 7 ; 5 8] are the first two columns of X(:,:,3) Unless R=1 and C=1 (which is true in one of the test cases) there will be at least some "intentional overlap" in the matrices that make up the correct solution.
That's what i ment. In the Problem Statement there is overlap, whether it is intentional or not, one can't decide. Looking at the test cases you can figure out, that when R/C>1 there must be overlap (in one of the cases "intentional" overlap). When R/C=1 there cant be any overlap. But if you think pratically (like it was an real world problem), one of both cases doesn´t make sense. If the overlap is needed the R/C=1 makes no sense and vice versa. To make my critism constructive: I would remove the R/C=1 cases and i'd add to the proplem, that there shall a be minimum number of matrices and there shall be at lest one element overlap. Excess overlap shall be between the last two matrices.
R/C=1 implies that R=C, not necessarily that both R=1 and C=1. When R=C and they are both greater than 1, there is intentional overlap because that is the way the problem is defined.
Believe it or not, this is a real world problem, Anselm. One of the people I work with asked me how to do exactly this particular calculation for some work he was doing. I gave him an answer (my answer to the problem, in fact) but figured that there had to be a better way to do it. A good way to determine the best way to do something is to submit it as a problem to Cody, and let the MATLAB geniuses do their thing.
The problem that you are describing sounds interesting. How about you write it up and submit it to Cody? They're always looking for new creators, after all.
to help clarify (not really) one application would be in convolutional networks, where for an input image M you might want to find the list X of all image blocks of size R*C (and R=C=1 is just a extreme case where you end up listing individual pixels)
Hi,
in my opinion this problem need a clarification, i.e. you have to move along columns and rows by incrementing only one row or one column each time.
It may sound obvious but I lost a lot of time solving the wrong problem, i.e. I moved along column first and row after, by incrementing each time by R and C.
I added a couple more sentences in the description to (hopefully) clear things up for people who were having the same problem you did, M.M.A.
Solution Comments
Show commentsProblem Recent Solvers352
Suggested Problems

4491 Solvers

1553 Solvers

204 Solvers

Reverse the elements of an array
1009 Solvers

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