フィルターのクリア

Store value in Matrix

1 回表示 (過去 30 日間)
Mystery Devil
Mystery Devil 2017 年 1 月 21 日
コメント済み: Star Strider 2017 年 1 月 21 日
Hello,
I want to store the value in the matrix form using for loop.
For example: Let's say I have a matrix A (2*2 matrix)
A = [1 2;
3 4]
But the output should be:
A = [1 0 0 0
1 2 0 0
1 2 3 0
1 2 3 4]
Can somebody tell me how to do this?
Thank you very much!

回答 (2 件)

Star Strider
Star Strider 2017 年 1 月 21 日
One approach:
A = [1 2;
3 4];
A = A';
B = repmat(A(:)',numel(A),1);
Result = tril(B)
Result =
1 0 0 0
1 2 0 0
1 2 3 0
1 2 3 4
  2 件のコメント
Mystery Devil
Mystery Devil 2017 年 1 月 21 日
Thank you very much! This is how I wanted. It worked!
Star Strider
Star Strider 2017 年 1 月 21 日
My pleasure!

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


Andrei Bobrov
Andrei Bobrov 2017 年 1 月 21 日
A = [1 2;
3 4];
out = tril(ones(numel(A),1)*reshape(A.',1,[]));
  1 件のコメント
Mystery Devil
Mystery Devil 2017 年 1 月 21 日
Thank you very much!

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by