Can I make the matrix of size n with only x elements known and rest all 0?

1 回表示 (過去 30 日間)
Jay Vaidya
Jay Vaidya 2020 年 11 月 14 日
回答済み: Setsuna Yuuki. 2020 年 11 月 14 日
I have a matrix that can have some x known values. But I want to make it of fixed length n. In other words, I want to append 0s and make one n sized matrix from x known elements and n-x 0s.
I think that this can be surely done by just one for loop, but it would be great if there is a way to do this with a one-line command.

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 11 月 14 日
No for loop is needed. You can use padarray(), but easier is
n = 20;
x = 1:12;
y = [x zeros(1, n-numel(x))]

その他の回答 (1 件)

Setsuna Yuuki.
Setsuna Yuuki. 2020 年 11 月 14 日
x = [1 2 3 4]
large = 8
x = [x zeros(1,large-length(x))]

カテゴリ

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

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by