how do I generate a spiral matrix which starts from first element

24 ビュー (過去 30 日間)
Hatake nara
Hatake nara 2019 年 6 月 9 日
コメント済み: Arshub 2022 年 5 月 16 日
there is a command which takes only one arguement as a dimension of a square spiral matrix.But I need a way to generate such a matrix which takes the numbers of rows and columns as inputs and it might not be a square
  1 件のコメント
darova
darova 2019 年 6 月 9 日
I don't understand. How this matrix should looks like? Can you please show some scheme?

サインインしてコメントする。

採用された回答

PIYUSH AGGARWAL
PIYUSH AGGARWAL 2019 年 6 月 9 日
編集済み: PIYUSH AGGARWAL 2019 年 6 月 10 日
You will have to write your own code for this.
A = zeros(3,6);%You can change this mxn to whatever dims you like, be it square(m=n) or rectangular matrix.
[m,n] = size(A);
%m = no of rows
%n = no of columns
%A = matrix to be filled with spiral values
%This code works fine. I commented an eg also below
top = 1;
bottom = m;
left = 1;
right = n; %To keep track of the four directions
value = 1;%Let us strat from 1
while true
%Top Row First
if left>right
break;
end
for i = left:right
A(top,i) = value;
value = value + 1;
end
top = top + 1;
%Then The RightMost Column
if top>bottom
break;
end
for i = top:bottom
A(i,right) = value;
value = value + 1;
end
right = right - 1;
%Then Bottom Row
if left>right
break;
end
for i = right:-1:left
A(bottom,i) = value;
value = value + 1;
end
bottom = bottom - 1;
%Then The Left Column
if top>bottom
break;
end
for i = bottom:-1:top
A(i,left) = value;
value = value + 1;
end
left = left + 1;
end
A
  5 件のコメント
Matsh
Matsh 2019 年 6 月 12 日
So the uzumaki_matrix is done then.What about dx_ball?:)
Arshub
Arshub 2022 年 5 月 16 日
@PIYUSH AGGARWAL Please, I hope that you can help me in modifying your code so that I enter an array from me like A= [2 3 5
12 13 14
19 0 4]
and a spiral display is made for it, then I want another code in which I can retrieve my original array. I would be grateful for your help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by