Error in GWO optimization algorithm

3 views (last 30 days)
Question: 1) writing (Best_pos(1:4)) is right? because it gives me error that the dimensions of the array being concatenated [ParsListMain is 4 elements].
2) Why the Best_pos gives me the ub of the code which is [3.7,0.05,2.8,3.5]?
Thanks in advance.
[Best_score,Best_pos,GWO_cg_curve]= GWO(20,100,[3.6,0.045,2.7,3.4],[3.7,0.05,2.8,3.5],4,@ee_battery_lse);
% Update Battery block with optimized parameters
Pars = reshape([ParsListMain; cellstr(num2str('Best_pos(1:4)'))'],1,[]);
for k=1:2:length(Pars)
evalin('base',[Pars{k} '=' Pars{k+1} ';'])
end
% Display optimized parameters
fprintf(['Optimized parameters for the battery main ' ...
'dialog tab are:\n']);
fprintf('\t%5s = %s\n', Pars{:});
clear i_data v_data t_data T_data Ts

Accepted Answer

Walter Roberson
Walter Roberson on 24 Apr 2022
cellstr(num2str('Best_pos(1:4)'))'
ans = 1×1 cell array
{'Best_pos(1:4)'}
'Best_pos(1:4)' is a character vector. num2str() applied to a character vector gives the character vector. cellstr() applied to the character vector wraps it in a cell array, giving you a 1 x 1 cell array.
You mention that ParsListMain is 1 x 4, but you do not indicate what data type it is. If it were numeric then in context of the [] with the 1 x 1 cell array, then the numeric content would be wrapped in a cell and you would get a 2 x 1 cell array. If, though, ParsListMain is a 1 x 4 cell array then you would be doing vertcat between a 1 x 4 cell and a 1 x 1 cell, and that would be an error.
If Best_pos happens to be a numeric row vector then in
Pars = reshape([ParsListMain; cellstr(num2str(Best_pos(1:4)))'],1,[]);
the indexing Best_pos(1:4) of the (assumed) row vector would give you a 1 x 4 numeric vector, and num2str() of a numeric vector gives you a 1 x whatever character vector, and you would be back to the same problem as before.
If Best_post happens to be any other shape other than row vector (in particular if it happens to be a column vector) then Best_pos(1:4) would give you a 4 x 1 numeric vector, and num2str() of that would give you a 4 x whatever character array, cellstr() of which would give you a 4 x 1 cell array. Which you then transpose to 1 x 4. vertical concatenation of that with a 1 x 4 cell array could work fine.
  5 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!