Replacing substring not working
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
ludolfusexe
am 27 Mai 2024
Kommentiert: ludolfusexe
am 27 Mai 2024
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)
newString = replace(toBeChanged, toBeRemoved,"{h(p,q_j)}");
Doing this with other smaller strings works just fine.
2 Kommentare
Mathieu NOE
am 27 Mai 2024
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)
newString = replace(toBeChanged, toBeRemoved,"{h(p,q_j)}")
Akzeptierte Antwort
Chunru
am 27 Mai 2024
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)
idx = find(i1 ~= i2)
s1(idx)
s2(idx)
isContained = contains(toBeChanged,toBeRemoved)
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)
newString = replace(toBeChanged, toBeRemoved,"{h(p,q_j)}")
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Entering Commands finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!