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일

0 개 추천

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일

0 개 추천

A = [1 2;
3 4];
out = tril(ones(numel(A),1)*reshape(A.',1,[]));

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2017년 1월 21일

댓글:

2017년 1월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by