How to scan matrix in spiral way?

조회 수: 16 (최근 30일)
Arshub
Arshub 2022년 5월 16일
댓글: DGM 2022년 5월 17일
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
Arshub
Arshub 2022년 5월 17일
@DGM Thanks alot for your help.
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에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by