How to fill a matrix in a specific way
1 view (last 30 days)
Show older comments
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];
%}
0 Comments
Answers (1)
Akira Agata
on 17 Feb 2017
Edited: Akira Agata
on 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);
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!