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.
采纳的回答
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)
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 个评论
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 Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!