フィルターのクリア

how can i combine two vector into one vector by the follwoing pattern ( and not use "For" Loop which take a lot of time , since this vector is a Million of elements ):

27 ビュー (過去 30 日間)
example :
a1 [ 1,2,3,4,5,6......]
a2 [ x,y,z,w,v,b,n,....]
a3 = [ 1,x,2,y,3,z,4,w,5,v,.....]
many thanks for the help
  2 件のコメント
Stephen23
Stephen23 2014 年 10 月 7 日
Are these two vectors of the same class?
Andrei Briciu
Andrei Briciu 2020 年 3 月 6 日
If
a1 and a2 are rows in a matrix a0,
then
a3=reshape(a0,1,numel(a0));

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

回答 (2 件)

Michael Haderlein
Michael Haderlein 2014 年 10 月 7 日
編集済み: Michael Haderlein 2014 年 10 月 7 日
One option
a3=[a1; a2];
a3=a3(:)';
This only works if a1, a2 are line vectors. Otherwise, take the transpose of each in the first line.
  3 件のコメント
Ellyn Gray
Ellyn Gray 2015 年 11 月 23 日
Thank you so much for this!! Very helpful explanations from you both
sravankumar v
sravankumar v 2017 年 8 月 11 日
can you please tell me how can i arrange the elements of three matrices alternatively in to another matrix using for loop?

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


Orion
Orion 2014 年 10 月 7 日
Hi,
the command a3=[a1; a2]; will put the a2 vector at the end of the a1 vector and not alternate the value of a1 and a2.
to make the pattern asked, you should do something like :
a1 = 1:10;
a2 = 21:30;
a3 = zeros(1,length(a1)*2);
a3(1:2:end-1) = a1;
a3(2:2:end) = a2;
this will work very fast even for a million element vector.
  4 件のコメント
Chris Liu
Chris Liu 2018 年 11 月 29 日
Thanks for the answer. So what's the case if and are matrix? When I do like that,
just change a3 = zeros(1,size(a1,2)*2);
it reports error: "unable to perform assignment because the left and right sides have a different number of elements." Thanks.
Chris Liu
Chris Liu 2018 年 11 月 29 日
sorry for my bad, I miss the zero matrix. It works fine under ur answer. thx

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by