Adding dummy zeroes in a matrix

I have several arrays of different lengths that need to combined into a single matrix of order n * n where n is the length of the longest array and dummy zeroes have to be added in those shorter arrays.For example,A=[1 2 3],B=[1 2 3 4] I want to combine A and B in a single matrix C =[1 2 3 0 1 2 3 4] .Note that a dummy zero has been added after 3 to make C a square.How do I achieve that?

2 件のコメント

Matt Tearle
Matt Tearle 2011 年 3 月 21 日
Do you want the result to be an n-by-n matrix, or a 1-by-n*n vector? Your example shows C as a vector, but you say "square". Also, if you mean C = [1 2 3 0;1 2 3 4], that's 2-by-4, not 4-by-4. So, would you actually want C = [1 2 3 0;1 2 3 4;0 0 0 0;0 0 0 0]?
And, finally, how do you have the vectors? Do you have a whole pile of vectors "A", "B", "C", ... "Q" already? Or will they be generated (eg in a loop)? Is there any pattern to the names, or do you have to manually reference them?
Anand Anand
Anand Anand 2011 年 3 月 21 日
Yeah ,you are right.It need not be a square matrix.I already have the pile of vectors and as such they are not named .I am refering to them as A,B,C and so on.As such,they are just numerous rows of numbers in a notepad

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

 採用された回答

Walter Roberson
Walter Roberson 2011 年 3 月 21 日

0 投票

n = max([numels(A), numels(B)]);
C = zeros(n,2);
C(1:numels(A),1) = A;
C(1:numels(B),2) = B;

1 件のコメント

Anand Anand
Anand Anand 2011 年 3 月 21 日
Thanks Walter

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSymbolic Math Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by