# Mathematica command question - matrix operator

• Jan 13th 2011, 05:31 AM
yosso
Mathematica command question - matrix operator
Hi.

Is it possible to prepare a set of commands, which lists elements of any size matrix in such sequence like this:

for 3x3 matrix:

$
\left(
\begin{array}{ccc}
a & b & c \\
d & e & f \\
g & h & i
\end{array}
\right)
$

the sequence is: $a, b, c, f, i, h, g, d, e$;

for matrix 4x4:

$
\left(
\begin{array}{cccc}
a & b & c & d \\
e & f & g & h \\
i & j & k & l \\
m & n & o & p
\end{array}
\right)
$

the sequence is: $a, b, c, d, h, l, p, o, n, m, i, e, f, g, k, j$,

etc.

So, the operator which forms the sequence, moves on matrix like a point drawing a spiral, independently of matrix’s size.

• Jan 13th 2011, 05:43 AM
Ackbeet
I'm sure it is possible. Mathematica is a full-blown programming language with loops, if's, increments, etc. One command that will be helpful is the Dimensions command. For example,

Dimensions[{{1, 2}, {3, 4}, {5, 6}}] returns

{3,2}.

You can access the first component of the result by indexing, which in Mathematica looks like this: [[i]]. So, for example,

Dimensions[{{1, 2}, {3, 4}, {5, 6}}][[1]] returns

3

and

Dimensions[{{1, 2}, {3, 4}, {5, 6}}][[2]] returns

2.

One more comment: indexing into a matrix looks like this:

A[[1]][[4]], which gives you the 1,4 element of the matrix. For example,

{{1, 2}, {3, 4}, {5, 6}}[[3]][[1]] returns

5.

I would set up some sort of a nested looping program to do what you are asking. Does this help?
• Jan 13th 2011, 06:42 AM
yosso
It will be great, if you send me any proposition of exact program to obtain results, which I have desired.

• Jan 13th 2011, 06:45 AM
Ackbeet
No, that's not the way we work around here. The helpers here are volunteers, and those asking questions are expected to put in the main effort to solve their problems. We're here to help people get unstuck, not to exhibit complete solutions to the problems.

So it's your turn to do something. What ideas do you have?
• Jan 13th 2011, 07:51 AM
yosso
So, if - for example - 3x3 matrix is

A = {{a, b, c}, {d, e, f}, {g, h, i}},

I may construct a list I've desired as

X = {A[[1, 1]], A[[1, 2]], A[[1, 3]], A[[2, 3]], A[[3, 3]], A[[3, 2]],
A[[3, 1]], A[[2, 1]], A[[2, 2]]}.

Is it possible to use it, for example with 'Which' and/or 'While' commands, to generalize the procedure? I'm trying to do it...
• Jan 13th 2011, 08:31 AM
Ackbeet
Excellent! Yes, your X list is precisely what you want. What you've got to do now is assign X programmatically. The While command would be very useful, I think. You could either append items to your X list, or you could pre-allocate the X list and use the ReplacePart command to replace the elements one at a time.

Couple of ideas you might think about:

1. Use East, South, West, and North as indicator directions for which direction you're traveling in the matrix.
2. Use some sort of limit to tell you when to stop going in a particular direction.

Where does all this get you?