M by M matrix

조회 수: 7 (최근 30일)
SomtoChukwu Ufondu
SomtoChukwu Ufondu 2017년 2월 16일
댓글: Image Analyst 2017년 2월 17일
Hi, all.
I'm a complete novice in Matlab. I need help in two similar but different areas:
Please, what's the code for creating an M by M matrix with 1 - (m1/m2) on the diagonal and zeros everywhere else?
Also, how can I make a matrix have 1,2,1 on every row in a diagonal, and zeros everywhere else? I hope you guys really understand what I am trying to explain here.
Thanks.
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 2월 17일
Sounds like homework...

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

답변 (2개)

John D'Errico
John D'Errico 2017년 2월 16일
편집: John D'Errico 2017년 2월 16일
If you are a complete novice, and have no cue how to do something like this, then why are you not reading the getting started tutorials? Surely they are there for a purpose?
If you want to know how to find how to do something, then learn to use the help tools, such as help and doc. As importantly, learn how to use the lookfor utility.
You want to create a diagonal matrix. So a good idea would be to try this:
lookfor diagonal
or perhaps,
lookfor identity
Another good idea is to try something like this:
help elmat
Look through those tools. It will show you most of the matrix creation utilities. You might find something that might help. Even better, you might see something that you will remember the next time you are feeling lost.
Finally, think about the problem you have posed.You wish to generate a matrix which is a scalar multiple of an identity matrix. So, IF you could generate an MxM identity matrix, then could you generate the matrix you desire? Which of the tools in the elmat directory would be most likely to generate an identity matrix? Surely there is something that does exactly that?
The point is, a complete novice needs to read the getting started documentation. But you also need to learn how to find the tools that will solve your future problems. It is all there, and easy to find, if only you look.
  댓글 수: 1
SomtoChukwu Ufondu
SomtoChukwu Ufondu 2017년 2월 16일
Thanks, man. Didn't know how to get to those tools. Now I know.

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


Image Analyst
Image Analyst 2017년 2월 16일
Not sure what m1 and m2 are, but if they are constants, a simple intuitive way for beginners is to use a for loop:
for k = 1 : M
yourArray(k, k) = 1-m1/m2;
end
  댓글 수: 2
SomtoChukwu Ufondu
SomtoChukwu Ufondu 2017년 2월 16일
Beautiful! Works perfect for the first question. Thanks a bunch.
Image Analyst
Image Analyst 2017년 2월 17일
A vectorized way of doing it:
m1=5; % Whatever you want.
m2=2.5; % Whatever you want.
k = 5; % Whatever you want.
yourArray = zeros(k);
yourArray(logical(eye(k))) = 1-m1/m2

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by