How to scan matrix in spiral way?

25 次查看(过去 30 天)
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 个评论
Arshub
Arshub 2022-5-16
编辑:Arshub 2022-5-16
@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 2022-5-16
@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.

请先登录,再进行评论。

采纳的回答

DGM
DGM 2022-5-16
编辑:DGM 2022-5-16
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 个评论
DGM
DGM 2022-5-17
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by