MATLAB Answers


convert two column matrices into one column matrix

Asked by simith
on 25 Mar 2018
Latest activity Commented on by simith
on 25 Mar 2018
Hello I want to combine two column vector matrices into one column vector matrix like the example
C must be like:
I need a general answer because I have many and large columns. Thank you


Sign in to comment.


2 Answers

Answer by Walter Roberson
on 25 Mar 2018
 Accepted Answer

nA = length(A);
nB = length(B);
C = zeros(nA + nB, 1);
nCommon = min(nA, nB);
C(1:2:nCommon*2-1) = A(1:nCommon);
C(2:2:nCommon*2) = B(1:nCommon);
if nA < nB
C(nCommon*2+1:end) = B(nCommon+1:end);
elseif nA > nB
C(nCommon*2+1:end) = A(nCommon+1:end);
%if the lengths were the same then everything was copied by the common processing

  1 Comment

thank you very worked proprely

Sign in to comment.

Answer by Roger Stafford on 25 Mar 2018

In the example you gave do this:
C = zeros(9,1);
C(1:2:end) = A;
C(2:2:end) = B;
For a general answer you need to define the generality you are seeking. Give us more information. For example, if the numbers in two vectors differs by more than one element, what do you wish as a result?

  1 Comment

thank you..your answer worked

Sign in to comment.