{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# snake - array as a vector Examples x = snake[1 2 3 4 5 6 7...

This preview shows page 1. Sign up to view the full content.

Sheet1 Page 1 % Write a function called snake that takes in an array and a number % indicating how many places to "snake" the array as described below. % Snaking the vector involves shifting the numbers down columns, and % appending the overflow onto the top of the next column. The overflow from % the last column must go into the top of the first column. % Hint: Use array linearization (it is very difficult unless you treat the
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: % array as a vector) % Examples: % x = snake([1 2 3 % 4 5 6 % 7 8 9], 2) % x -> [6 4 5 % 9 7 8 % 1 2 3] % x2 = snake([4 7 9 % 3 1 8], 1) % x2 -> [8 3 1 % 4 7 9] function Z = snake(A,num) [row col] = size(A) Z = A x = row * col if num > x num = num - x end for i = 1:(x-num) Z(i + num) = A(i) end for i = (x-num+1):x Z((x-(i+num)) * -1) = A(i) end...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online