Shifting the outer loop of an array
Info
This question is closed. Reopen it to edit or answer.
Show older comments
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 Comment
Walter Roberson
on 1 Jun 2015
Answers (1)
Walter Roberson
on 1 Jun 2015
0 votes
Go back to the answer you were given in http://uk.mathworks.com/matlabcentral/answers/221718-only-shifting-the-outer-loop
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)
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!