how to combine matrixes

1 回表示 (過去 30 日間)
Sharen H
Sharen H 2013 年 2 月 27 日
I have to combine two matrices for example
a=[10 20 30 40 50 60]
b=[11 22 33 44 55 66]
based on some random seed for example r =2 ,combine 2 values from the matrix a and b to form a output matrix
c=[10 20 11 22 30 40 33 44 50 60 55 66]
Please help me

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 27 日
a=[10 20 30 40 50 60]
b=[11 22 33 44 55 66]
r=2
out=[reshape(a,r,[]);reshape(b,r,[])]
out=out(:)'
  1 件のコメント
Sharen H
Sharen H 2013 年 2 月 27 日
Thanks

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 27 日
編集済み: Azzi Abdelmalek 2013 年 2 月 27 日
General case
a=[10 20 30 40 50 60];
b=[11 22 33 44 55 66];
r=randi(numel(a));
id=r-mod(numel(a),r);
if id~=r
a(end+1:end+id)=nan;
b(end+1:end+id)=nan;
end
out=[reshape(a,r,[]);reshape(b,r,[])];
out=out(:)';
out(isnan(out))=[]
  2 件のコメント
Sharen H
Sharen H 2013 年 2 月 27 日
this code gives an error called Undefined function or method 'randi' for input arguments of type 'double' what does it mean
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 27 日
Your version of Matlab does not support rndi, then instead of r=randi(numel(a)); use
[~ ,r]=max(rand(1,numel(a)))

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by