Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Loop these commands

1 view
Skip to first unread message

Chris

unread,
Feb 5, 2010, 11:57:06 AM2/5/10
to
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

Oleg Komarov

unread,
Feb 5, 2010, 12:29:05 PM2/5/10
to
"Chris "
This piece of code has no sense until you won't specify what do you mean by 'array'.

Oleg

Chris

unread,
Feb 5, 2010, 12:39:04 PM2/5/10
to
> 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.

ImageAnalyst

unread,
Feb 5, 2010, 12:39:32 PM2/5/10
to
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?

Chris

unread,
Feb 5, 2010, 1:15:06 PM2/5/10
to
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.

Steven Lord

unread,
Feb 5, 2010, 1:17:05 PM2/5/10
to

"Chris " <ridere...@hotmail.com> wrote in message
news:hkhl3o$53j$1...@fred.mathworks.com...

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


0 new messages