Azzera filtri
Azzera filtri

How to put data in vector into matrix

1 visualizzazione (ultimi 30 giorni)
nora elfiky
nora elfiky il 4 Gen 2017
Commentato: Roger Stafford il 5 Gen 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)?

Risposte (2)

Walter Roberson
Walter Roberson il 4 Gen 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 il 5 Gen 2017
Modificato: Roger Stafford il 5 Gen 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 Commento
Roger Stafford
Roger Stafford il 5 Gen 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).

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by