This is the assignment I was given: he 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
So far I have where it shifts by two positions but I don't know how to get the jut the outer loop to shift. Any hints?
A = [1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20]
mover = 2
B = circshift(A,mover,2)

댓글 수: 5

Gina Barlage
Gina Barlage 2015년 6월 1일
A = [1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20]; mover = 2;
A_out = zeros(size(A)); 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) A_inner = [A(2,2:end-1), A(end-1, 2:end-1)]; ctr = 0; for ii = A(1, :) A_out(ii) = A_out_2(1,1:5); end A_out
I have now found all the numbers on the outer ring and the numbers in the inner ring. Can I use a loop to put these numbers in a place? or can I use the zeros function to make two arrays and add them together. I do not know where to go from here.
No, the outerIndices suggested by Matt J are indices, not the values as in your code. Stay at this code:
Values = A(outerIndices)
A(outerIndices) = ...
Now all you have to do is to insert the circshift 'ed Values instead of the "...". You do not need any loops.
When I did the above with the A(outerIndices) = circshift(outerindices,mover,2) I got this:
A =
11 3 3 4 19
6 16 4 9 20
1 12 17 5 15
2 17 18 18 10
Walter Roberson
Walter Roberson 2015년 6월 2일
Don't shift the indices, shift the content. See where I said "insert the circshift'ed Values"? Values, not indices
Gina Barlage
Gina Barlage 2015년 6월 2일
Ok I get it now thanks so much!

댓글을 달려면 로그인하십시오.

답변 (1개)

Matt J
Matt J 2015년 5월 31일
편집: Matt J 2015년 5월 31일

0 개 추천

As a hint, the following should give you the Linear Indices of the outer edges of the matrix in clockwise order,
I=reshape(1:m*n,m,n);
outerIndices=[I(1,:),I(2:end-1,end).',I(end,end:-1:1),I(end-1:-1:2,1).'];

댓글 수: 2

Gina Barlage
Gina Barlage 2015년 5월 31일
Thanks so much! I understand how this works but how do I incorporate the outerindices and the inside of array A?
Values = A(outerIndices)
A(outerIndices) = ...

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2015년 5월 31일

댓글:

2015년 6월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by