Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Shifting the outer loop of an array

조회 수: 2 (최근 30일)
Gina Barlage
Gina Barlage 2015년 6월 1일
마감: MATLAB Answer Bot 2021년 8월 20일
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일
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)

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by