Creating a super diagonal matrix.

조회 수: 10 (최근 30일)
Mike
Mike 2015년 8월 10일
댓글: Mike 2015년 8월 10일
Please what's a neat way of creating a matrix that looks like this?
[1 2 3 4 5 0 0 0 0 0; 0 1 2 3 4 5 0 0 0 0; 0 0 1 2 3 4 5 0 0 0; 0 0 0 1 2 3 4 5 0 0; 0 0 0 0 1 2 3 4 5 0]
Thank you.
  댓글 수: 1
Cedric
Cedric 2015년 8월 10일
Is it just a small example or is it the real matrix that you need to build? If it is a small example, what will be the size of the real one, and what is the size of the non-zero band?

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

채택된 답변

John D'Errico
John D'Errico 2015년 8월 10일
A gazillion ways. A few suggestions...
1. Simple is to create this as a circulant matrix. I've got a tool for that on the file exchange as I recall.
2. Or, use spdiags. This has the virtue of making it a sparse matrix.
3. Or create it as the sum of multiple diagonal matrices, each built using diag.
4. A matrix with the pattern you show can be built with a call to sparse, where you need to create a list of non-zero elements, and the row and column indices for each. This can be easier than it sounds. Again, you get a sparse result.
5. The simple pattern shown can easily enough be built using meshgrid and a test, possibly with a call to tril or triu afterwards.
Why not play with some or more of the ideas I posed above? This is the way to learn, by playing around with a goal in mind.
  댓글 수: 1
Mike
Mike 2015년 8월 10일
It's a small example, the matrix I'm trying to build is a 501 x 501 matrix with the non-zeros being 1:1:100. I'll go through your suggestions to see which I can use. Thanks John!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by