MATLAB Answers

convert two column matrices into one column matrix

3 ビュー (過去 30 日間)
simith
simith 2018 年 3 月 25 日
コメント済み: simith 2018 年 3 月 25 日
Hello I want to combine two column vector matrices into one column vector matrix like the example
A=[1;4;6;7;8]
B=[10;21;11;9]
C must be like:
C=[1;10;4;21;6;11;7;9;8]
I need a general answer because I have many and large columns. Thank you

採用された回答

Walter Roberson
Walter Roberson 2018 年 3 月 25 日
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
end
  1 件のコメント
simith
simith 2018 年 3 月 25 日
thank you very much..it worked proprely

サインインしてコメントする。

その他の回答 (1 件)

Roger Stafford
Roger Stafford 2018 年 3 月 25 日
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 件のコメント
simith
simith 2018 年 3 月 25 日
thank you..your answer worked

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by