Shifting the outer loop of an array

I was given an assignment to:
The script should assume an m-by-n array A (m, n >= 2) is assigned in the Command Window. The value mover should also be assigned. The script should rotate all of the values on the outer loop of the array by mover spots clockwise and call the output A_out.
Example executions follow:
>> A = [1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20]; mover = 2;
>> script25
A_out =
11 6 1 2 3
16 7 8 9 4
17 12 13 14 5
18 19 20 15 10
I wrote script that works for the above example but I am not sure how to make I more general for an array put into the Command Window. Here is what I have:
A = [1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20];
mover = 2;
outerIndices = [A(1,:), A(2:end-1,end).', A(end,end:-1:1),A(end-1:-1:2,1).'];
A_out_2 = circshift(outerIndices,mover,2)
ctr = 0;
for ii = A_out_2(1:5)
for jj = 1
ctr = ctr + 1
A(jj,ctr) = ii
end
end
ctr = 1;
for kk = A_out_2(6:8)
for jj = 5
ctr = ctr+1;
A(ctr, jj) = kk
end
end
ctr = 5;
for mm = A_out_2(9:12)
for jj = 4
ctr = ctr-1;
A(jj,ctr) = mm
end
end
ctr = 4
for nn = A_out_2(13:end)
for jj = 1
ctr = ctr -1
A(ctr, jj) = nn
end
end
A

回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 6 月 1 日

0 投票

Use the approach shown there to create OuterIndices: they showed you indices and instead in the code here you have pulled out the content.
Once you have the indices, the content at those locations is A(OuterIndices). You can circshift the content and assign it back to A(OuterIndices)

この質問は閉じられています。

質問済み:

2015 年 6 月 1 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by