Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Shifting the outer loop of an array

2 visualizaciones (últimos 30 días)
Gina Barlage
Gina Barlage el 1 de Jun. de 2015
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
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

Respuestas (1)

Walter Roberson
Walter Roberson el 1 de Jun. de 2015
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)

La pregunta está cerrada.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by