How to put data in vector into matrix

3 Ansichten (letzte 30 Tage)
nora elfiky
nora elfiky am 4 Jan. 2017
Kommentiert: Roger Stafford am 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)?

Antworten (2)

Walter Roberson
Walter Roberson am 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 am 5 Jan. 2017
Bearbeitet: Roger Stafford am 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 Kommentar
Roger Stafford
Roger Stafford am 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).

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by