From: Chris on
I wrote this code line to transform a given array, n times. But this only works for 3 columns and I need it to work for m columns and repeat n times. I've spent a lot of time on this and need some advice at least. Thanks in advance!


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
From: Oleg Komarov on
"Chris "
> I wrote this code line to transform a given array, n times. But this only works for 3 columns and I need it to work for m columns and repeat n times. I've spent a lot of time on this and need some advice at least. Thanks in advance!
>
>
> 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
This piece of code has no sense until you won't specify what do you mean by 'array'.

Oleg
From: Chris on
> This piece of code has no sense until you won't specify what do you mean by 'array'.
>
> 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.
From: ImageAnalyst on
I can't figure it out either. I guess I'm having trouble finding the
code in the dense forest of comments. ;-)
Are you somehow trying to do what circshift() already does?
Why do you have two "end" lines? Is there a for loop that is missing
somehow?
How is "n" even being used in your twist function?
From: Chris on
I'll try to be more clear:

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.