Borrar filtros
Borrar filtros

How to fill a matrix in a specific way

1 visualización (últimos 30 días)
Andrew Poissant
Andrew Poissant el 17 de Feb. de 2017
Editada: Akira Agata el 17 de Feb. de 2017
I have a 5x5 matrix of 0's defined as P below. What I want to do is fill it out to become the commented out matrix also seen below. What is the best way of going about populating the matrix with the pattern seen in the commented out matrix? Let me know if the pattern isn't clear. You essentially have a pattern of u 0.1 h and that moves throughout the matrix going down the columns.
N = 5;
P = zeros(N,N);
h = 5.2;
u = 1.7;
%{
P = [0.1 h 0 0 0 ; ...
u 0.1 h 0 0; ...
0 u 0.1 h 0; ...
0 0 u 0.1 h; ...
0 0 0 u 0.1];
%}

Respuestas (1)

Akira Agata
Akira Agata el 17 de Feb. de 2017
Editada: Akira Agata el 17 de Feb. de 2017
I think diag function will help.
https://www.mathworks.com/help/matlab/ref/diag.html
Please try the following script.
N = 5;
h = 5.2;
u = 1.7;
P = diag(repmat(0.1,1,5)) + diag(repmat(u,1,4),-1) + diag(repmat(h,1,4),1);

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by