How to write diagonal matrix in deep learning array
Show older comments
I have this matrix
D = zeros(M, M + 1);
D(1:end-1, 1:end-2) = diag((1/(2*h)) * ones(M-1, 1));
D(1:end-1, 3:end) = diag((-1/(2*h)) * ones(M-1, 1));
D(end, end-1:end) = (1/h) * [1,-1];
and I am writing this in a deep learning dlarray. But I am getting this below error
Undefined function 'diag' for input arguments of type 'dlarray'.
what is the best or another way to write this diagonal matrix for dlarray?
2 Comments
"what is the best or another way to write this diagonal matrix for dlarray?"
Simply define the array first as you have and then convert using dlarray. No need of using a for loop.
M = 10;
h = 5;
D = zeros(M, M + 1);
D(1:end-1, 1:end-2) = diag((1/(2*h)) * ones(M-1, 1));
D(1:end-1, 3:end) = diag((-1/(2*h)) * ones(M-1, 1));
D(end, end-1:end) = (1/h) * [1,-1];
D
E = dlarray(D)
Muhammad
on 17 Jan 2024
Accepted Answer
More Answers (0)
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!