How can I put in an array the elements near the center?
I have a matrix 100x100. I want to save the 8 elements around the center of the matrix in an array. Then I want to save the 16 elements around the 8 elements and so on till I finish the matrix.
+ + + + + + - - - + + - + - + + - - - + + + + + +
Explaining it in a better way, I want to save the '+' in the middle in an array. Then, I want to save all the '-' in another array. Finally I want to save all the '+' in a third array. And so on till I complete the 100x100 matrix (that I attached).
Thanks in advance.
1 comentario
Respuestas (2)
- Decide for a method to define exactly, which is the "center" in a matrix with an even side length.
- Create a for loop from 0 to the half side length
- Then 4 for loops use the value from the first loop to collect the elements.
- Store the elements in a cell array.
0 comentarios
Dario - you could create a recursive function that "peels" off the outer "layer" of your input matrix. You would then pass a subset of the initial matrix (less the first row, first column, last row, and last column) into your function to get peel off the next "layer". You would continue in this matter until either the input matrix is empty (implying that your initial square matrix has an even number of rows/columns) or is a scalar (implying that your initial square matrix has an odd number of rows/column). The algorithm might be something like
- If matrix is empty then return empty cell.
- If matrix is scalar then return cell containing matrix (centre).
- If matrix has the same number of rows and columns, then peel off the outer layer (first row, last row, first column, second column) and save as array in a cell. Concatenate this cell with the output of your call to the recursive function passing in your matrix less the "layer" that you peeled away.
0 comentarios
Ver también
Categorías
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!