Clear Filters
Clear Filters

remove null terms from a string and adjust signs

2 views (last 30 days)
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?

Accepted Answer

John BG
John BG on 6 Jun 2016
Edited: John BG on 11 Jun 2016
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 Comments
S H
S H on 10 Jun 2016
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

Sign in to comment.

More Answers (1)

Jos (10584)
Jos (10584) on 9 Jun 2016
str1 = strrep(str,'(0)*','')
str2 = strrep(str1,'*(0)','')

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!