フィルターのクリア

remove null terms from a string and adjust signs

2 ビュー (過去 30 日間)
S H
S H 2016 年 6 月 4 日
編集済み: John BG 2016 年 6 月 11 日
Hi,
I have a string with more than thousands of terms in it. A small sample part of the string is
'...(G5)*((G6)*(G2+G4)+(G4)*(G2))+(G2)*((0)*(G4+G6)+(0)*(G3+G7)+(G4+G6)*(G1))+(G4)*((0)*(G6)+(G6)*(G7)-(0)*(G6))-(G9)*((G40+G60)*(0)-(G24+G26)*(G3)+(G14+G16)*(0))...'
I do not want to convert this string to symbolic expressions and I want to process it as a string because the location of each term is meaningful and important and the conversion to symbolic expression disregards it.
What I need to do is to remove all null terms from the string and adjust signs when negatives (or combination of negatives and positives) are multiplied together. the answer for the given part of the main string should be:
'...(G5)*((G6)*(G2+G4)+(G4)*(G2))+(G2)*((G4+G6)*(G1))+(G4)*((G6)*(G7))+(G9)*((G24+G26)*(G3))...'
Could you please show me how to do it using a very efficient code that I can plug in my m file?

採用された回答

John BG
John BG 2016 年 6 月 6 日
編集済み: John BG 2016 年 6 月 11 日
Now, I humbly claim my answer as accepted:
str1='(G5)*((G6)*(G2+G4)+(G4)*(G2))+(G2)*((0)*(G4+G6)+(0)*(G3+G7)+(G4+G6)*(G1))+(G4)*((0)*(G6)+(G6)*(G7)-(0)*(G6))-(G9)*((G40+G60)*(0)-(G24+G26)*(G3)+(G14+G16)*(0))';
loc=strfind(str1,'(0)');loc2=loc+1;loc3=loc2+1; % find locations of '(0)' only
loc4=strfind(str1,'*(0)');
ch4={0}
for k=1:length(loc4)
ch4=[ch4 {0}]
end
for k=1:1:length(loc4)
if str1(loc4(k)-1)==')' % spotting all characters related to each multiplying term
no=0;nc=1;
p4=loc4(k)-1;
ch4{k}=p4;
while (nc>no) && (p4>0) %
p4=p4-1;
if str1(p4)=='('
no=no+1; end;
if str1(p4)==')'
nc=nc+1; end;
ch4{k}=[p4 ch4{k}];
end
end
if str1(loc4(k)+4)=='+' || str1(loc4(k)+4)=='-' % spotting sign after: *(0)- *(0)+
ch4{k}=[ch4{k} loc4(k)+4]
end
if (str1(p4-1)=='+' || str1(p4-1)=='-') % spotting sign before term: +(..)*(0) -(..)*(0)
ch4{k}=[p4-1 ch4{k}]
end
end
N4=0
for k=1:length(ch4)
N=ch4{k}
N4=[N4 N]
end
N4(N4==0)=[]
loc42=N4 % contains all multiplying terms in front of *(0)
loc
loc43=loc4-3
loc5=strfind(str1,'(0)*');
ch5={0}
for k=1:length(loc5)
ch5=[ch5 {0}]
end
for k=1:1:length(loc5)
if str1(loc5(k)+4)=='('
no2=1;nc2=0;
p5=loc5(k)+4;
ch5{k}=p5;
while (no2>nc2) && (p5<length(str1)) %
p5=p5+1;
if str1(p5)=='('
no2=no2+1; end;
if str1(p5)==')'
nc2=nc2+1; end;
ch5{k}=[ch5{k} p5];
end
end
if str1(loc5(k)-1)=='+' || str1(loc5(k)-1)=='-' % spotting sign ahead: -(0)* +(0)*
ch5{k}=[loc5(k)-1 ch5{k}]
end
if (str1(p5+1)=='+' || str1(p5+1)=='-') % spotting sign before term: (0)*(..)+ (0)*(..)-
ch5{k}=[ch5{k} p5+1]
end
end
N5=0
for k=1:length(ch5)
N=ch5{k}
N5=[N5 N]
end
N5(N5==0)=[]
loc52=N5 % contains all multiplying terms ibehind all *(0)
loc53=loc5+3
% erasing all null related characters
str1(union(union(union(union(union(union(union(loc,loc2),loc3),loc42),loc52),loc4),loc5),loc53))=[];
so far the sample seems clean:
str1 =
(G5)*((G6)*(G2+G4)+(G4)*(G2))+(G2)*((G4+G6)*(G1))+(G4)*((G6)*(G7))-(G9)*((G24+G26)*(G3))
would it be possible to run this along the entire sequence?
If you find this answer of any help solving your question,
please mark it as accepted answer,
thanks in advance
John
  3 件のコメント
John BG
John BG 2016 年 6 月 8 日
you are right, let me have a look.
S H
S H 2016 年 6 月 10 日
This question can be solved by following steps:
1. identify one of the (0)* or *(0) terms.
2. count the following or leading parentheses until match if found
3. remove the whole thing and apply sign adjustment

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

その他の回答 (1 件)

Jos (10584)
Jos (10584) 2016 年 6 月 9 日
str1 = strrep(str,'(0)*','')
str2 = strrep(str1,'*(0)','')

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by