How to scan matrix in spiral way?
25 views (last 30 days)
Show older comments
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
Accepted Answer
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)
The result could also be used as an indices into another array.
mg = magic(n)
mg(sp)
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
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.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!