MATLAB Answers

error:Deminsion 2 is fixed on left hand side but varies on right([1x1]}~=([1x:?])

5 views (last 30 days)
While convertio=ng my code fro matlab to c++ I am showing some error; which says Deminsion 2 is fixed on left hand side but varies on right([1x1]}~=([1x:?])
this is my code which i was trying to convert where T is 1x401 and Y is 1x21
function summ = u_sum(T,Y)
summ = 0;
for i=1:20
lmd = (2*i+1)*pi;
a = 4/((lmd)^3);
b = sin(lmd*Y);
c = exp(-(lmd^2)*T);
summ = summ + (a*b*c);
end
end

Accepted Answer

Mathieu NOE
Mathieu NOE on 10 Nov 2020
hi
there is something wrong in your code
a, b,c have different dimensions so the product a*b*c is not possible (summ = summ + (a*b*c); )
Name Size Bytes Class
T 1x401 3208 double array
Y 1x21 168 double array
a 1x1 8 double array
b 1x21 168 double array
c 1x401 3208 double array
i 1x1 8 double array
lmd 1x1 8 double array
summ 1x1 8 double array
  3 Comments

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 10 Nov 2020
summ = 0;
That is a scalar. Once that assignment is made, the variable cannot grow larger..
b = sin(lmd*Y);
That uses all of Y, so b would be 1x21.
summ = summ + (a*b*c);
scalar plus scalar times vector times scalar gives vector right hand side. But you try to assign to summ which has been created as a scalar. When you are doing code generation, variables cannot grow unless you use special forms.
If you want a vector output then initialize summ to zeroes(size(Y)).
I suspect that you might have wanted to index Y in the loop

Community Treasure Hunt

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

Start Hunting!