MATLAB Answers

Size mismatch error on dimension 2: expected 1, but actual size is 3 (Matlab Coder)

52 views (last 30 days)
Hi, I have this line in my code
Edited:
function test_1()
% load('test1.mat');
% A(1:3,1:8) = 9
coder.varsize('SimCoreOut.V_cell');
SimCoreOut=struct('V_cell',zeros(102,3),'I_string',0.0);
Temp.V_fix_cell=[4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201
4.1201 4.1201 4.1201];
Temp.R_fix_cell = [0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128
0.0128 0.0128 0.0128];
SimCoreOut.I_string = -1.3074e-11;
SimCoreIn.Bal_block=zeros(102,1);
SimCoreIn.R_bal_block=ones(102,1);
SimCoreOut.V_cell=(((sum(Temp.V_fix_cell./Temp.R_fix_cell,2))-SimCoreOut.I_string)./(sum(1./Temp.R_fix_cell,2)+SimCoreIn.Bal_block./SimCoreIn.R_bal_block)).*ones(102,3);
I removed For loop so You can Ignore (k)
SimCoreOut(k).V_cell=(((sum(Temp(k).V_fix_cell./Temp(k).R_fix_cell,2))-SimCoreOut(k).I_string)./(sum(1./Temp(k).R_fix_cell,2)+SimCoreIn(k).Bal_block./SimCoreIn(k).R_bal_block)).*ones(SimCoreIn(k).Ns,SimCoreIn(k).Np);
I will breakdown this line for clarity.
Size of:
SimCoreOut(k).V_cell = 102 x 3
(sum(Temp(k).V_fix_cell./Temp(k).R_fix_cell,2)) = 102 x 1
SimCoreOut(k).I_string = some double let say 50.6
sum(1./Temp(k).R_fix_cell,2) = 102 x 1
SimCoreIn(k).Bal_block./SimCoreIn(k).R_bal_block = 102 x 1
(((sum(Temp(k).V_fix_cell./Temp(k).R_fix_cell,2))-SimCoreOut(k).I_string)./(sum(1./Temp(k).R_fix_cell,2)+SimCoreIn(k).Bal_block./SimCoreIn(k).R_bal_block)) = 102 x 1
Till this part whole size is 102 x 1 which is now going to multiplied by ones(SimCoreIn(k).Ns,SimCoreIn(k).Np)
Size of ones(SimCoreIn(k).Ns,SimCoreIn(k).Np) = 102 x 3
So (102 x 1).*(102 x 3) gives (102 x 3) right.
When I am trying to convert to C++ using Matlabcoder it is showing this error.
I used coder.varsize but still it is showing the same error.
Things I tried:
In coder we have to intialize whole structure befor subscripting right, so I declared SimCoreOut(k).V_cell as zeros(102,3), If i Changes to zeros(102,1) then it is showing dimension mismatch error in other part of the code,which doesn't even compile in matlab. (It will be something like matrix dimensions does not match for multiplication/division)
I tried using brackets to rhs side still it is treating as size 102 x 1.
Can anyone explain, what would be the cause of this error.
To sum up I will give sample example
LHS = ((something1*something2)./(something3))*(something4)
lets say ((something1*something2)./(something3)) is 102 x 1 and something4 size is 102 x 3
So coder is expecting LHS size equal to bold part size but actual size should be size of ((something1*something2)./(something3))*(something4).
  2 Comments
Naga Manoj Kumar Lakkoju
Naga Manoj Kumar Lakkoju on 11 Apr 2021
Hi @Darshan Ramakant Bhat Thanks for your reply, I edited my Question by hard coded inputs. Maybe you will get nice picture this time.

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 11 Apr 2021
Edited: Bruno Luong on 11 Apr 2021
Size of ones(SimCoreIn(k).Ns,SimCoreIn(k).Np) = 102 x 3
So (102 x 1).*(102 x 3) gives (102 x 3) right.
Right but coder won't accept auto-expansion.
You must replace the statement
SimCoreOut(k).V_cell=(((sum(Temp(k).V_fix_cell./Temp(k).R_fix_cell,2))-SimCoreOut(k).I_string)./(sum(1./Temp(k).R_fix_cell,2)+SimCoreIn(k).Bal_block./SimCoreIn(k).R_bal_block)).*ones(SimCoreIn(k).Ns,SimCoreIn(k).Np);
using bsxfun function.
BTW why don't you break the expression for better readability.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!