Unable to perform assignment because the left and right sides have a different number of elements.

1 view (last 30 days)
Hi, I'm new to MATLAB. I have troubles figuring out how to get rid of this error message: "Unable to perform assignment because the left and right sides have a different number of elements."
Ps_bedA = 10^3*(-5.007261711E-15*T_bedA^6 + 1.249032418E-11*T_bedA^5 - 1.135454979E-08*T_bedA^4 + 5.164337223E- 06*T_bedA^3 - 1.275119405E-03*T_bedA^2 + 1.643475641E-01*T_bedA - 8.706432224E+00);
RH_A = exp((17.27*(Tdp_a)/(237.7+(Tdp_a)))-(17.27*(T_bedA-273)/(237.7+(T_bedA-273))));
K_bedA = A*exp(n*(Qst-h_fg_cond)/(Rw*Y(2)));
kads_A = 18*((RH_A*Ps_bedA)/(2*pi*18*R*Y(2))^0.5) * exp(-(Ea/(R*T_bedA)))*189.6;
w_bedA_const = (kads_A*(RH_A)^n) + ((kads_A*(K_bedA^-1)*(1-(RH_A)^n)));
dy(1) = (kads_A*(K_bedA^n) - w_bedA_const*w_bedA/wm)*wm;
Unable to perform assignment because the left and right sides have a different number of elements.
Error in ADsgw12kw80>ddydwdt (line 454)
dy(1) = (kads_A*(K_bedA^n) - w_bedA_const*w_bedA/wm)*wm;
I have tried searching for similar questions on this but I have not been able to do anything about it. Please help.

Answers (3)

KSSV
KSSV on 6 Dec 2022
Check the result or size of:
(kads_A*(K_bedA^n) - w_bedA_const*w_bedA/wm)*wm
It seems, the above is not a scalar or a single number, You are trying to save it into a single number.
Iether use:
dy{1} = (kads_A*(K_bedA^n) - w_bedA_const*w_bedA/wm)*wm;
Or try to explore the element by element operations.

Steven Lord
Steven Lord on 6 Dec 2022
MATLAB has a suite of debugging tools you can use to investigate errors like this. One of the ones I use most frequently is the ability to set breakpoints, places where MATLAB will pause execution and allow me to examine the values of variables or expressions to make sure that they contain the values they should contain.
I recommend setting a breakpoint on that line of code and running your code. When you reach the breakpoint, do as @KSSV recommended and evaluate the right-hand side of that expression. It's not going to be the same size as dy(1) which has 1 element. That problem is like trying to put multiple eggs in one cup in an egg carton (and no, you're not allowed to scramble the eggs to get them to fit.) You need to make sure you have the same number of cups on the left side as you do eggs on the right (or, to break the analogy slightly, 1 egg on the right can be duplicated to fill any number of cups on the left.)
I agree with KSSV again that you probably are using matrix operations (which operate on matrices as a whole) when you should be using array operations (which operate element-wise.) See this documentation page for an explanation. Those operations can yield quite different results:
A = [1 2 3; 4 5 6; 7 8 9]
A = 3×3
1 2 3 4 5 6 7 8 9
matrixOperation = A*A % square the matrix
matrixOperation = 3×3
30 36 42 66 81 96 102 126 150
elementwiseOperation = A.*A % square each element
elementwiseOperation = 3×3
1 4 9 16 25 36 49 64 81

Image Analyst
Image Analyst on 6 Dec 2022
Check out the FAQ for a list of common error messages and their resolution:

Community Treasure Hunt

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

Start Hunting!