フィルターのクリア

Expand a column vector to a matrix without using for loop

17 ビュー (過去 30 日間)
bugatti79
bugatti79 2013 年 10 月 1 日
回答済み: Jan 2013 年 10 月 1 日
Hi Folks,
Is there a built in command to expand a (n*1) to an (n*n) matrix without filling the matrix using a for loop? All entries are of the same scaler value. Thanks in advanced.

採用された回答

ES
ES 2013 年 10 月 1 日
ResultMatrix=repmat(a,1,n)

その他の回答 (1 件)

Jan
Jan 2013 年 10 月 1 日
Or:
a = rand(10, 1);
A = a(:, ones(1, n));
Under some circumstances this is not created explicitly, e.g. in:
a = rand(10, 1);
b = rand(1, 10);
C = a(:, ones(1, 10)) + b(ones(1, 10), :);
D = bsxfun(@plus, a, b);
While bsxfun is smart enough not to create the temporarily expanded arrays, the calculation of C can profit from the same trick, when the JIT recognizes this. While this happened in all of my test cases until R2011a, since 2011b the JIT seems to handle this expanding less efficient.
Please note, that the JIT is not documented and my opinion about what happens inside is based only on the observed runtime and the used memory.

カテゴリ

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