1

I have seen until now that when someone wants to read the elements of a matrix using a spiral one by one they always mean starting outwards and slowly moving to the center using an MxM matrix. How would we do the opposite, start from a random point inside the matrix and using a "spiraling" path to read all the elements.

I am doing the tests using Matlab.

Example.

mat=
  1 2 3
  4 5 6
  7 8 9 

If lets say we were to start from mat(3,1) spiraling clockwise then we would have

vec=
  7, 8, 4, 5, 6, 9, 1, 2, 3

and if we started from mat(2,2) then

vec=
5, 6, 9, 8, 7, 4, 1, 2, 3
ealiaj
  • 1,525
  • 1
  • 15
  • 25
  • I'd guess spiraling outward should be like spiraling inward, but with reverse direction. Don't see what exactly you need to know, you already described a way to spiral around. Unless you have more precise requirements you expect your path to satisfy, your way is as good as any, so I don't see what kind of answer you expect. I don't see what you are asking. – MvG Apr 28 '14 at 20:57
  • Well as i said usually those implementations involve a MxM matrix and a spiral that always starts from the same position. What I am trying to do is create a vector that will follow the path created by a spiral that can start from any point we want. – ealiaj Apr 28 '14 at 21:01
  • @green_leaf: If there are already implementations solving your problem partially, please link them. – Daniel Apr 28 '14 at 21:25
  • I stumbled upon this question http://stackoverflow.com/questions/726756/print-two-dimensional-array-in-spiral-order and since every solution (or question) for that matter starts the spiral in one of the corners until the center of the matrix I decided to start working on the opposite problem so as the spiral goes outwards from a central point as I explain in the question – ealiaj Apr 28 '14 at 21:33
  • So what - other than outline the problem here - have you done? And what do you need help with? – Schorsch Apr 28 '14 at 22:49
  • @Schorsch not much really, I don't have a code that is working only a main structure sort of. The idea is to have a for loop from 1:M*N, and in that for loop i will use a separate function that will each time move one step to the desired position until it reaches a number called Xsteps. Now Xsteps will increase in size every time mod(current loop, 2)=0. – ealiaj Apr 29 '14 at 07:22
  • And since it will be a MxN matrix and the starting position can be whatever we choose I will also have to check when that position exceeds the matrix dimensions. I mainly want to see how others would approach this as I am not a programmer and most likely my code even when working will be bad. – ealiaj Apr 29 '14 at 07:42

1 Answers1

2

One possible approach:

mat = [1 2 3; 4 5 6; 7 8 9];
M = length(mat); % Assuming mat is always MxM

r = 3;
c = 1;

temp = spiral(2 * M - 1);
temp = temp(M - r + 1:end - r + 1, M - c + 1:end - c + 1);
[~, idx] = sort(temp(:));
vec = mat(idx).'

Result running with r = 3 and c = 1

vec =

     7     8     4     5     6     9     1     2     3

Result running with r = 2 and c = 2

vec =

     5     6     9     8     7     4     1     2     3
Rafael Monteiro
  • 4,509
  • 1
  • 16
  • 28