How to put data in vector into matrix

1 view (last 30 days)
nora elfiky
nora elfiky on 4 Jan 2017
Commented: Roger Stafford on 5 Jan 2017
How to put data in vector into matrix and then changes the position of bits in the matrix. It can be applied in row wise or column wise(Spiral transposition)?

Answers (2)

Walter Roberson
Walter Roberson on 4 Jan 2017
You can reshape() the vector unless the number of elements is prime.
Consider using the buffer() function from signal processing

Roger Stafford
Roger Stafford on 5 Jan 2017
Edited: Roger Stafford on 5 Jan 2017
The following is an example of inserting a row vector, V, into an m by n matrix, M, in a “spiral transposition”. The spiral in this case starts at M(1,1) and spirals inward in a counterclockwise direction. The row vector V must be (at least) m*n in length.
M = zeros(m,n);
a = 0; b = m; c = 1; d = n; f = 0;
while true
a = a+1; e = f+1; f = e+b-a;
if a>b|n==0, break, end
M(a:b,c) = V(e:f).'; % ?
c = c+1; e = f+1; f = e+d-c;
if c>d, break, end
M(b,c:d) = V(e:f);
b = b-1; e = f+1; f = e+b-a;
if a>b, break, end
M(b:-1:a,d) = V(e:f).'; % ?
d = d-1; e = f+1; f = e+d-c;
if c>d, break, end
M(a,d:-1:c) = V(e:f);
end
(Note: The transpose operators at the two question marks are probably not necessary in your system.)
  1 Comment
Roger Stafford
Roger Stafford on 5 Jan 2017
The above spiral is of course only one of sixteen possible spiral types, consisting: (1) of whether the spiral begins or terminates at a corner, (2) of four possible such corners, and (3) of either counterclockwise or clockwise directions. It should be noted that the above code can readily be modified to allow all possible sixteen types to be generated by appropriate combinations of matlab’s ‘fliplr’, ‘flipud’, and ’transpose’ operations carried out afterwards on M, and by possibly initially reversing V, as with V = V(m*n:-1:1).

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!