MATLAB: Struggling with array dot operator

arraydimensionmatrix

I've initialized
t = 0:0.1:20;
L = zeros(4,4,length(t));
I'm trying to assign values to L as follows:
t=0:0.1:20;
for i =1:length(t),
L(:,:,i) = D*diag([(t.^2+1).^2*(sin(t).^2), (t.^2+1).^2*(sin(2*t).^2), (t.^2+1).^2*(sin(3*t).^2), (t.^2+1).^2*(sin(4*t).^2), (t.^2+1).^2*(sin(5*t).^2)])*D';
end
D is a constant matrix. Essentially, there is a different L for each time instant. When I execute this, it gives
Error using *
Inner matrix dimensions must agree.
I'm tried so many combinations of the dot operators since this is a long operation.
Can someone help identify the problem?
Thanks a lot.

Best Answer

Define the t-data before the for loop
T=0:0.1:20;
for i =1:length(T),
t=T(i);
L(:,:,i) = D*diag([(t.^2+1).*(t.^2+1).*(sin(t).^2) (t.^2+1).*(t.^2+1).*
(sin(2*t).^2) (t.^2+1).*(t.^2+1).*(sin(3*t).^2) (t.^2+1).*(t.^2+1).*(sin(4*t).^2)
(t.^2+1).*(t.^2+1).*(sin(5*t).^2)])*D';
end
Also, it is a good ideas to use commas to explictly separate entries of a vector/matrix.
Related Question