Only Shifting the outer loop

2 visualizaciones (últimos 30 días)
Gina Barlage
Gina Barlage el 31 de Mayo de 2015
Comentada: Gina Barlage el 2 de Jun. de 2015
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 comentarios
Walter Roberson
Walter Roberson el 2 de Jun. de 2015
Don't shift the indices, shift the content. See where I said "insert the circshift'ed Values"? Values, not indices
Gina Barlage
Gina Barlage el 2 de Jun. de 2015
Ok I get it now thanks so much!

Iniciar sesión para comentar.

Respuestas (1)

Matt J
Matt J el 31 de Mayo de 2015
Editada: Matt J el 31 de Mayo de 2015
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 comentarios
Gina Barlage
Gina Barlage el 31 de Mayo de 2015
Thanks so much! I understand how this works but how do I incorporate the outerindices and the inside of array A?
Jan
Jan el 1 de Jun. de 2015
Values = A(outerIndices)
A(outerIndices) = ...

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by