I'm having a problem. Here is my code:
UFR = 0.042;
alpha = 0.1;
N = 20;
%Wilson function
for p = 1:N
for q = 1:N
W = SWfunction(p,q,alpha,UFR);
end
end
w = inv(W);
for t = 1:s
for h = 1:N
M(:,h) = exp(-(h)*R(t,h));
MU(:,h) = exp(-UFR*h);
Z(:,h) = zeros;
m = transpose(M);
mu = transpose(MU);
z = transpose(Z);
z(:,t) = w.*(m-mu);
end
end
When I run my code matlab tells me matrix dimensions do not agree when calculating z(:,t). But here's what confuses me, when I run the code without that line the matrix dimensions are fine for multiplication (20x20 with 20x1) but when I have that line it changes the variables m and mu to 1x1 matrices... I need z to be a matrix that performs that calculation for all t. Can someone help me?

 채택된 답변

Roger Stafford
Roger Stafford 2014년 12월 6일

2 개 추천

If 'SWfunction' produces a scalar value, then 'w' will be a scalar, that is 1 x 1, since it is being overwritten 400 times in your first nested loops. That would produce an error at the line
z(:,t) = w.*(m-mu);
You need to use p and q to index 'w' values, as in w(p,q), so as to produce a 20 x 20 matrix.

댓글 수: 3

Thank you! That worked to some extent - now it no longer has a problem with the matrix dimension mismatch and creates a matrix z with the size I need. But it only populates the last column of the matrix, the rest of the matrix is zeros. Any idea why this might be happening?
UFR = 0.042;
alpha = 0.1;
N = 20;
%Wilson function
for p = 1:N
for q = 1:N
W = SWfunction(p,q,alpha,UFR);
end
end
w = inv(W);
for t = 1:s
for h = 1:N
M(t,h) = exp(-(h)*R(t,h));
MU(t,h) = exp(-UFR*h);
Z(t,h) = zeros;
m = transpose(M);
mu = transpose(MU);
z = transpose(Z);
mmu(h,t) = m(h,t)-mu(h,t);
z(:,t) = w(p,q)*mmu(:,t);
end
end
Thanks again for taking the time to read and reply! :)
In the code you show in the above comment the quantity 'w' is still a 1 x 1 scalar. You need W(p,q) instead of W in the line
W = SWfunction(p,q,alpha,UFR);
A second problem is that in the nested for loops that create M and MU you are taking the transpose of arrays that are only partially computed. That makes no sense. You need to be creating the 'm', 'mmu' and 'z' arrays in a later action after M and MU are fully computed.
Also the line
z(:,t) = w(p,q)*mmu(:,t);
makes no sense. The p and q indices have long since become obsolete since they refer to indices in the first set of for loops.
Catarina
Catarina 2014년 12월 9일
Ok I've figured it out now! Thank you for your help! :)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2014년 12월 6일

댓글:

2014년 12월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by