function a = twist(array , n)
out = array;
out(2:end,1) = array(1:end-1,1);
in = array;
outvec = [ in(1:end, 1), in(end:-1:1, 2), in(1:end, 3)];
outvec = [ in(1:end, 1)', in(end:-1:1, 2)', in(1:end, 3)'];
outans = [outvec(end), outvec(1:end-1)];
end
d = reshape(outans,size(out));
a = reverseCols(d);
end
Oleg
array is just a mxn matrix of numbers. This function rotates the numbers down the first column, up the second column, down the third column, and so on. When numbers reach the top or bottom of the column they jump to the next column. The last number in the matrix jumps to the first number. I want this function to repeat x times... The code works but only 1 time.
function a = twist(some_matrix, n)
> some_matrix(2:end,1) = some_matrix(1:end-1,1); %shifts first column down
> in = some_matrix;
> outvec = [ in(1:end, 1), in(end:-1:1, 2), in(1:end, 3)]; %rotates column 2
> outvec = [ in(1:end, 1)', in(end:-1:1, 2)', in(1:end, 3)']; %linearizes outvec
> outans = [outvec(end), outvec(1:end-1)]; %rotates the numbers to the last value
%is first and the other numbers slide down
>
> d = reshape(outans,size(out)); %forms new matrix
> a = reverseCols(d); %function reverseCols flips every even column
> end
I wrote the reverseCols function previously and wanted to use it here. I want this to repeat n number of times but cannot figure out the loop to do it. Trying to use indexing as much as possible.
Thanks for the time.
I _think_ I understand what you're trying to do, but to confirm ... what do
you want this code to do on some small examples? What would you want A, B,
C, and D to be after executing the following code?
M = [1 2 3;4 5 6];
N = [7 8;9 10; 11 12];
A = twist(M, 2)
B = twist(M, 3)
C = twist(N, 2)
D = twist(N, 4)
--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ