How to scan matrix in spiral way?

25 views (last 30 days)
Arshub
Arshub on 16 May 2022
Commented: DGM on 17 May 2022
I enter an array from me or user defined like A= [1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18]
and a spiral display is made for it like:
A=[1 2 3 4 5 6
14 15 16 17 18 7
13 12 11 10 9 8]
, then I want another code in which I can retrieve my original array agin in A' matrix. I need code for two processes and I would be grateful for your help.
  3 Comments
Arshub
Arshub on 16 May 2022
Edited: Arshub on 16 May 2022
@Image Analyst I've seen previous answers for the spiral scan, but I couldn't find the reverse process because I needed it for an image encryption process.
This is not my homework, but I am a master's student and I build my algorithm using Matlab, and I need this code in my work.
Arshub
Arshub on 16 May 2022
@Image AnalysI asked before, but I did not get the desired response and the right answer, so I decided to ask the question again in a short way.

Sign in to comment.

Accepted Answer

DGM
DGM on 16 May 2022
Edited: DGM on 16 May 2022
There is a demo function that can generate NxN arrays in an outward spiral. That could be used (with some arithmetic and flips/transposes) to generate NxN spirals in other directions/orientations.
n = 5;
sp = spiral(n)
sp = 5×5
21 22 23 24 25 20 7 8 9 10 19 6 1 2 11 18 5 4 3 12 17 16 15 14 13
The result could also be used as an indices into another array.
mg = magic(n)
mg = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
mg(sp)
ans = 5×5
15 16 22 3 9 2 5 6 12 18 21 24 17 23 1 20 11 10 4 7 14 8 25 19 13
However, that's strictly a square array. If you want something different, you'll have to do something else.
As one possibility, MIMT has a tool for reshaping images using spiral devectorizing. It could be used for this.
A = reshape(1:18,6,3).'
A =
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
B = jellyroll(A.').'
B =
1 2 3 4 5 6
14 15 16 17 18 7
13 12 11 10 9 8
Arecovered = jellyroll(B.','reverse').'
Arecovered =
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
Obviously, I can't run that here without MIMT, but this should show that jellyroll() does work on nonsquare matrices. The direction and orientation can be specified with the appropriate options. It just happens that this one case works fine with the defaults.
I should point out though that jellyroll() is intended to be a novelty for use in manual image processing workflows where computational efficiency is unimportant. Keep that in mind if you stick it in a short loop with tiny arrays.
  8 Comments
Arshub
Arshub on 17 May 2022
@DGM Thanks alot for your help.
DGM
DGM on 17 May 2022
If and when you find that the answer satisfies your needs, you can click "Accept". That way the post is labeled as having an accepted answer and may be more likely to be found by the next person with a similar question.

Sign in to comment.

More Answers (0)

Products


Release

R2014a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!