Question: Spiral Matrix Procedure in Maple

July 24 2014 AM 10
Maple
2

I am wanting to write a procedure to take a square matrix and have it output a spiral matrix.

for example; M:=Matrix(3,[[1,1,1],[2,2,2],[3,3,3]]);

would turn into

S:=Matrix(3,[[1,1,1],[3,3,2],[3,2,2]]);

Starting in the top left corner and each row follows around clockwise until you reach the middle.

My first thought was I need to be able to call each element (m_i,j) from a matrix and tell it where to go. I could write a different procedure for each square matrix assigning where the elements in the matrix each should move to. Since I could not get it to work for n.

Here is what i have for a 3x3 matrix


Spiral := proc(a1,a2,a3,b1,b2,b3,c1,c2,c3)
local M,S;
M:=Matrix(3,[[a1,a2,a3],[b1,b2,b3],[c1,c2,c3]]);
S:=Matrix(3,[[a1,a2,a3],[c2,c3,b1],[c1,b3,b2]]);
print(M);
print(S);
end:
Spiral(1,2,3,4,5,6,7,8,9);

It is very difficult for me to find information about Matrices in Maple. Any hint on using Maple would be appreciated. Thank you.

Please Wait...