Error in GWO optimization algorithm

조회 수: 1 (최근 30일)
Hend Mostafa
Hend Mostafa 2022년 4월 24일
댓글: Hend Mostafa 2022년 4월 27일
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

채택된 답변

Walter Roberson
Walter Roberson 2022년 4월 24일
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
Walter Roberson
Walter Roberson 2022년 4월 25일
If ParsListMain is a cell array of character vectors, then ParsListMain(1:4) would be {'Vnom', 'R1', 'AH', 'V1'} which would be a cell array of character vector. It is not clear what the intended output of num2str({'Vnom', 'R1', 'AH', 'V1'}) would be.
Pars0 = reshape([[ParsListMain ParsListDyn ParsListTemp]; cellstr(num2str([InitGuessMain InitGuessDyn InitGuessTemp]'))'],1,[]);
where the Init* variables are each numeric vectors.
Hend Mostafa
Hend Mostafa 2022년 4월 27일
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Inputs에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by