Alternative to EVAL string expressions in optimization

1 回表示 (過去 30 日間)
Kylekk
Kylekk 2022 年 5 月 30 日
コメント済み: Kylekk 2022 年 5 月 31 日
Hi,
I have a huge matrix, each element is a string like "16*q^2+4*q", i call it new_sol_array, now i want to define multiple optimconstr object with each row one set of constraints. This is what i did:
constr= optimconstr(M,N);
for i=1:rows
for j =1:column
constr(i,j)=eval(new_sol_array(i,j)+'==0');
end
end
As you would see, this is extremely slow. Is there a better alternative to do the same thing? i have read some of the answers about eval on this website yet did not find anything relevant enough to my problem.
Thank you so much!!!

採用された回答

Walter Roberson
Walter Roberson 2022 年 5 月 31 日
Instead of eval() everything, use file operations to emit a file along the lines of
constr = [
EQN, EQN,... EQN
EQN, EQN,... EQN
]
along with a function header. Then "clear" the name of the file. Then invoke the file as a function passing in q and whatever else is required
This reduces the burden by not needing to eval() each one individually.
  4 件のコメント
Stephen23
Stephen23 2022 年 5 月 31 日
編集済み: Stephen23 2022 年 5 月 31 日
  1. yes
  2. no
  3. no
Did you try writing a function file and calling it, as Walter Roberson described?
Kylekk
Kylekk 2022 年 5 月 31 日
Thank you Stephen, I did not do that simply because I don't get the idea. What kind of function file are you talking about? I have only a string array, why would a function takes variable names as input help?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by