How to fill a matrix in a specific way

1 Ansicht (letzte 30 Tage)
Andrew Poissant
Andrew Poissant am 17 Feb. 2017
Bearbeitet: Akira Agata am 17 Feb. 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];
%}

Antworten (1)

Akira Agata
Akira Agata am 17 Feb. 2017
Bearbeitet: Akira Agata am 17 Feb. 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);

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by