Interweaving vectors

2019-01-14 17:31发布

问题:

I would like to interweave two vectors in MATLAB. In fact, I'd actually just like to add a zero between each element, but I figured I'd ask the question in such a way that I'd learn how to apply this to other situations.

My specific application: I'd like to take a vector (e.g. [1 2 3]) and output [0 1 0 2 0 3].

The wider question: How would I do this with two different vectors, e.g. [1 2 3] and [9 8 7] interweaving to produce [9 1 8 2 7 3].

Any help greatly appreciated, in either or both of the above questions.

回答1:

Here's some code that will accomplish what you want:

nums   = rand(1,3)
output = zeros(1,2*numel(nums));
output(2:2:end) = nums

The output is the following:

nums =

    0.9134    0.6324    0.0975


output =

         0    0.9134         0    0.6324         0    0.0975

Following the style of the other two answers, you could get your desired zeros output with the following:

nums = rand(1,3);
reshape([zeros(size(nums));nums],1,[])

Obviously, nums should be replaced with the vector you'd like to use. As mentioned, you should make sure it's a row vector before calling reshape.



回答2:

I'd make use of the internal layout of MATLAB vectors: They are stored column-major, that is, values in a column change fastest. To interleave two vectors a and b, simply do:

ar = a(:).';    % make sure ar is a row vector
br = b(:).';    % make sure br is a row vector
A = [ar;br];   % concatenate them vertically
c = A(:);      % flatten the result


回答3:

Try

reshape([[9 8 7];[1 2 3]],1,[])


回答4:

For the most simple case there's a quite "elegant" solution with a one-liner as described in this answer to a similar question.

a = [9 8 7];
b = [1 2 3];
output = kron(a, [1 0]) + kron(b, [0 1]);

As stated in the answer be aware that this is less efficient however might serve for a more general purpose than just interleaving as it uses the Kronecker product that is well-defined between two matrices.



回答5:

I wrote a MATLAB function that's on the File Exchange site (Interleave Vectors or Matrices) that does exactly what you want and more. Just download the .m file and put the file in the same directory as your other .m files, or copy and paste the function into your program.

This function interleaves any number of vectors or matrices by row or column. If the input are just vectors, there is no need to specify orientation. Extra elements/rows/columns are appended on the end of the output matrix. The other answers provided are very specific for vectors of equal length or require making sure the orientation of vectors is correct.

Examples of how to use the function:

1) Interleaving rows of matrices

A = [1 2; 3 4] B = [5 6;7 8]

C = interleave2(A, B, 'row') 
C = [1 2 
     5 6 
     3 4 
     7 8]

2) Interleaving columns of matrices

C = interleave2(A, B, 'col') 
C = [1 5 2 6 
     3 7 4 8]

3) Interleaving vectors (Note: input vectors need not be same orientation)

A = [1 2 3 4] B = [5 6 7 8 9]' 
C = interleave2(A, B) 
C = [1 5 2 6 3 7 4 8 9]'

4) Interleaving >2 matrices

A = [1 2;3 4] B = [5 6;7 8] 
C = [9 10;11 12] D = [13 14;15 16]

E = interleave2(A, B, C, D, 'col') 
E = [1 5 9 13 2 6 10 14 
     3 7 11 15 4 8 12 16]

5) Interleaving columns of 2 matrices with unequal columns

A = [1 2;3 4] 
B = [5 6 7 8;9 10 11 12] 
C = interleave2(A, B, 'col') 
C = [1 5 2 6 7 8 
     3 9 4 10 11 12] 

6) Interleaving >2 vectors of unequal lengths

A = [1 2 3 4] B = [5 6 7] 
C = [8 9 10 11 12 13] 
D = interleave2(A, B, C) 
D = [1 5 8 2 6 9 3 7 10 4 11 12 13]


回答6:

Octave is a free, open source reverse engineering of MatLab where the functionality and language are the same in most cases. However, if you find yourself coding in Octave, you can use the below to interleave two (real valued) row vectors x and y:

[x;y](:)'

Caveat: this is Octave code and doesn't work in MatLab.



标签: matlab vector