matlab - Convert a matrix to a vector using a spiral path -
I have seen so far that when one wants to use the elements of a matrix one after the other, then they are always It starts by using an MXM matrix, going outwards and slowly towards the center, how we do the opposite, starting from a random point inside the matrix, and a "spiraling" to read all the elements Using the path.
I am testing using matlab.
Example.
mat = 1 2 3 4 5 6 7 8 9 If you say we want to start with mat (3,1) clock If we increase our direction then we will have vec = 7, 8, 4, 5, 6, 9, 1, 2, 3
and if We start with mat (2,2) then
vec = 5, 6, 9, 8, 7, 4, 1, 2, 3
< / Div>
A possible approach:
mat = [1 2 3; 4 5 6; 7 8 9]; M = length (mat); Assuming the mat is always MXM R = 3; C = 1; Temp = Spiral (2 * M-1); Temporary = Temporary (M - R + 1: End - R + 1, M - C + 1: End - C + 1); [~, Idx] = sort (temp (:)); Vec = mat (idx). Results running with r = 3
and C = 1
vec = 7 8 4
5 6 9 2 2
Results r = 2
and c = 2
< Running with code> VCK = 5 6 9 8 7 4 1 2 3
Comments
Post a Comment