M by M matrix

8 visualizaciones (últimos 30 días)
SomtoChukwu Ufondu
SomtoChukwu Ufondu el 16 de Feb. de 2017
Comentada: Image Analyst el 17 de Feb. de 2017
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 comentario
Walter Roberson
Walter Roberson el 17 de Feb. de 2017
Sounds like homework...

Iniciar sesión para comentar.

Respuestas (2)

John D'Errico
John D'Errico el 16 de Feb. de 2017
Editada: John D'Errico el 16 de Feb. de 2017
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 comentario
SomtoChukwu Ufondu
SomtoChukwu Ufondu el 16 de Feb. de 2017
Thanks, man. Didn't know how to get to those tools. Now I know.

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 16 de Feb. de 2017
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 comentarios
SomtoChukwu Ufondu
SomtoChukwu Ufondu el 16 de Feb. de 2017
Beautiful! Works perfect for the first question. Thanks a bunch.
Image Analyst
Image Analyst el 17 de Feb. de 2017
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

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by