Replacing substring not working

4 ビュー (過去 30 日間)
ludolfusexe
ludolfusexe 2024 年 5 月 27 日
コメント済み: ludolfusexe 2024 年 5 月 27 日
Why isn't this working?
toBeChanged = "\dot{h}_j(\phi_j,x,y) = \delta _{u}-\delta _{l}\,{\mathrm{e}}^{-\frac{{\left(q_{\mathrm{jx}}-x\right)}^2+{\left(q_{\mathrm{jy}}-y\right)}^2}{2\,\sigma ^2}}\,\phi_j";
toBeRemoved = "{\mathrm{e}}^{-\frac{{\left(p_{\mathrm{jx}}-x\right)}^2+{\left(p_{\mathrm{jy}}-y\right)}^2}{2\,\sigma ^2}}";
isContained = contains(toBeChanged,toBeRemoved)
isContained = logical
0
newString = replace(toBeChanged, toBeRemoved,"{h(p,q_j)}");
Doing this with other smaller strings works just fine.
  2 件のコメント
Mathieu NOE
Mathieu NOE 2024 年 5 月 27 日
you made a slight error when doing the copy paste ...
toBeChanged contains left(q_
and
toBeRemoved contains left(p_
if you make the correction , it works
toBeChanged = "\dot{h}_j(\phi_j,x,y) = \delta _{u}-\delta _{l}\,{\mathrm{e}}^{-\frac{{\left(q_{\mathrm{jx}}-x\right)}^2+{\left(q_{\mathrm{jy}}-y\right)}^2}{2\,\sigma ^2}}\,\phi_j";
toBeRemoved = "{\mathrm{e}}^{-\frac{{\left(q_{\mathrm{jx}}-x\right)}^2+{\left(q_{\mathrm{jy}}-y\right)}^2}{2\,\sigma ^2}}";
isContained = contains(toBeChanged,toBeRemoved)
isContained = logical
1
newString = replace(toBeChanged, toBeRemoved,"{h(p,q_j)}")
newString = "\dot{h}_j(\phi_j,x,y) = \delta _{u}-\delta _{l}\,{h(p,q_j)}\,\phi_j"
ludolfusexe
ludolfusexe 2024 年 5 月 27 日
Wow, definitely needed a break, thank you!

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

採用された回答

Chunru
Chunru 2024 年 5 月 27 日
toBeChanged = "\dot{h}_j(\phi_j,x,y) = \delta _{u}-\delta _{l}\,{\mathrm{e}}^{-\frac{{\left(q_{\mathrm{jx}}-x\right)}^2+{\left(q_{\mathrm{jy}}-y\right)}^2}{2\,\sigma ^2}}\,\phi_j";
% | |
toBeRemoved = "{\mathrm{e}}^{-\frac{{\left(p_{\mathrm{jx}}-x\right)}^2+{\left(p_{\mathrm{jy}}-y\right)}^2}{2\,\sigma ^2}}";
s1 = extractAfter(toBeChanged, "\delta _{l}\,");
n = strlength(toBeRemoved);
s1 = char(s1); s2 = char(toBeRemoved);
i1 = int16(s1(1:n)); i2 = int16(s2)
i2 = 1x106
123 92 109 97 116 104 114 109 123 101 125 125 94 123 45 92 102 114 97 99 123 123 92 108 101 102 116 40 112 95
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
idx = find(i1 ~= i2)
idx = 1x2
29 64
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
s1(idx)
ans = 'qq'
s2(idx)
ans = 'pp'
isContained = contains(toBeChanged,toBeRemoved)
isContained = logical
0
toBeChanged = "\dot{h}_j(\phi_j,x,y) = \delta _{u}-\delta _{l}\,{\mathrm{e}}^{-\frac{{\left(q_{\mathrm{jx}}-x\right)}^2+{\left(q_{\mathrm{jy}}-y\right)}^2}{2\,\sigma ^2}}\,\phi_j"
toBeChanged = "\dot{h}_j(\phi_j,x,y) = \delta _{u}-\delta _{l}\,{\mathrm{e}}^{-\frac{{\left(q_{\mathrm{jx}}-x\right)}^2+{\left(q_{\mathrm{jy}}-y\right)}^2}{2\,\sigma ^2}}\,\phi_j"
toBeRemoved = "{\mathrm{e}}^{-\frac{{\left(q_{\mathrm{jx}}-x\right)}^2+{\left(q_{\mathrm{jy}}-y\right)}^2}{2\,\sigma ^2}}"
toBeRemoved = "{\mathrm{e}}^{-\frac{{\left(q_{\mathrm{jx}}-x\right)}^2+{\left(q_{\mathrm{jy}}-y\right)}^2}{2\,\sigma ^2}}"
isContained = contains(toBeChanged,toBeRemoved)
isContained = logical
1
newString = replace(toBeChanged, toBeRemoved,"{h(p,q_j)}")
newString = "\dot{h}_j(\phi_j,x,y) = \delta _{u}-\delta _{l}\,{h(p,q_j)}\,\phi_j"

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by