How can i multiply each element of structured array
7 views (last 30 days)
Show older comments
I am trying to do an optimization problem in matlab.
below is the objective function .Here second and third term Price and Nb_dch1V, Price and Nb_ch1V are struct arrays (1 x 1 struct)
with two fields :
time 288 x 1 double
signal 1 x 1 struct
Signal again has two fields :
values 288 x 241 double
dimensions 241 .
hence is this objective function correct?
I am getting error
An error occurred while running the simulation and the simulation was terminated
Caused by:
- Objective must be a scalar OptimizationExpression or a struct containing a scalar OptimizationExpression.
Please help
prob.Objective =dt*Price'*PbattupsV +dt*Price'*PbattdchV*Nb_dch1V'+ dt*Price'*PbattchV*Nb_ch1V' ;
0 Comments
Answers (2)
Alan Weiss
on 28 Jul 2021
Edited: Alan Weiss
on 28 Jul 2021
You need to extract the correct elements from your structures before using them. I am not sure what you mean to have as an objective. Maybe this:
var1 = dt*Price.time'*PbattupsV; % Check that this is a scalar expression
var2 = dt*Price.time'*PbattdchV*Nb_dch1V.time'; % Check that this is a scalar expression
var3 = dt*Price.time'*PbattchV*Nb_ch1V.time'; % Check that this is a scalar expression
prob.Objective = var1 + var2 + var3;
Alan Weiss
MATLAB mathematical toolbox documentation
3 Comments
Alan Weiss
on 29 Jul 2021
I'm sorry that you are having these problems, but you must realize that all of them are due to size mismatches in your structures. I am not sure why you are using structures; it might be easier if you simply used matrix variables.
In any case, if you have a 1-by-241 variable and a 241-by-1 variable that you are trying to add, then you should convert both to the same size. There are many ways of converting vectors. If M is the 1-by-241 variable, you can convert it to a 241-by-1 variable in any of the following ways:
M = M(:); % might be the best
M = M';
M = M.';
M = reshape(M,241,1);
M = reshape(M,[],1); % might be the best
M = reshape(M,[241,1]);
Use whichever you like.
Alan Weiss
MATLAB mathematical toolbox documentation
4 Comments
Alan Weiss
on 1 Aug 2021
I suggest that you learn to use the debugger. Set a break point before you create prob.Objective. Carefully examine all your variables. The objective must be a scalar.
Alan Weiss
MATLAB mathematical toolbox documentation
See Also
Categories
Find more on Manual Performance Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!