how can i add two matrix vertically with different column

3 ビュー (過去 30 日間)
eli karimi
eli karimi 2019 年 9 月 12 日
コメント済み: eli karimi 2019 年 9 月 12 日
Hello,
how can i add two matrix vertically with different column?
please help
thank you
like this example:
A=[1 3 6 8 9]
B=[1 2]
Result= 1 3 6 8 9
1 2 0 0 0
  2 件のコメント
darova
darova 2019 年 9 月 12 日
What have you tried already? What problems do you have?
eli karimi
eli karimi 2019 年 9 月 12 日
i used of vertcat and get me this error:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.

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

採用された回答

madhan ravi
madhan ravi 2019 年 9 月 12 日
編集済み: madhan ravi 2019 年 9 月 12 日
m = max(numel(A),numel(B));
% Method 1
Wanted = reshape([A(:);B(:);zeros(m-numel(B),1)],[],2).'
% Method 2
w = cellfun(@(x)[x,zeros(1,m-numel(x))],{A,B},'un',0);
Wanted = cat(1,w{:})
% Method 3
Wanted = zeros(2,m);
Wanted(1,:) = A;
Wanted(2,1:numel(B)) = B
% Method 4
Wanted = [A;B,zeros(1,m-numel(B))]
  1 件のコメント
eli karimi
eli karimi 2019 年 9 月 12 日
Thank you very much for helping me.
It was very useful

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

その他の回答 (2 件)

Adam Danz
Adam Danz 2019 年 9 月 12 日
Bpad = padarray(B,[0,numel(A)-numel(B)],'post');
C = [A;Bpad]
  2 件のコメント
eli karimi
eli karimi 2019 年 9 月 12 日
Thank you very much for helping me.
It was very useful
Adam Danz
Adam Danz 2019 年 9 月 12 日
Glad I could help!

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


Bruno Luong
Bruno Luong 2019 年 9 月 12 日
I would do with the old well-served for-loop
C={A B}; % put the list of your matrices here
m=cellfun('size',C,1);
n=cellfun('size',C,2);
Results=zeros(sum(m),max(n));
r=0;
for k=1:length(C);
Results(r+(1:m(k)),1:n(k))=C{k};
r=r+m(k);
end
  1 件のコメント
eli karimi
eli karimi 2019 年 9 月 12 日
Thank you very much for helping me.

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by