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.

 채택된 답변

Matt J
Matt J 2017년 12월 17일
편집: Matt J 2017년 12월 17일

1 개 추천

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.

댓글 수: 4

Deepayan Bhadra
Deepayan Bhadra 2017년 12월 17일
Hi Matt, I've made the question more compact but the error persists since it seems not because of 't' definition but rather due to dot array multiplication. Could you check that? Thanks.
Matt J
Matt J 2017년 12월 17일
What are the dimensions of D?
Matt J
Matt J 2017년 12월 17일
편집: Matt J 2017년 12월 17일
This works for me, if D is 5x5,
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
Deepayan Bhadra
Deepayan Bhadra 2017년 12월 17일
This is working now :) Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2017년 12월 17일

0 개 추천

In the computation for L you should be using t(i) not t

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2017년 12월 17일

댓글:

2017년 12월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by