Hello all,
I want to create this matrix
A=[0 0 1 1 1;0 1 1 1 0;1 1 1 0 0]
How can I get ?
Thank you.

 채택된 답변

Star Strider
Star Strider 2014년 4월 13일
편집: Star Strider 2014년 4월 13일

0 개 추천

One way:
v = [0 0 1 1 1 ];
A = [v; circshift(v,[0 -1]); circshift(v,[0 -2])]

댓글 수: 2

Thewall
Thewall 2014년 4월 13일
Thanks for your answer.
However, I have many columns and rows. I think I should shift 100 times of v vector. How can I do this?
Star Strider
Star Strider 2014년 4월 13일
편집: Star Strider 2014년 4월 13일
My pleasure!
Use a for loop. If all you want to do is to shift the columns as you described for each row, change my example code to the more generalised version:
v = [0 0 1 1 1 ];
nrows = 3;
for k1 = 1:nrows
A(k1,:) = circshift(v,[0 1-k1]);
end
Change v to the vector you want, and change the number of rows you want to create (denoted here as the value of the variable nrows) to create your matrix.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

질문:

2014년 4월 13일

편집:

2014년 4월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by