Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Replacing string variables that satisfy some criteria by some other string variables in a cell vector when there are repetitions
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Dear all,
I have the following matrix
Matrix={ 'country' 'values1' 'text'
'wr' [ 152] [ NaN]
'wr' [ 45152] [ NaN]
'wr' [ 654152] [ NaN]
'wr' [ 15552] 'derf'
'wr' [ 155682] 'dggyyd'
'wr' [ 15445352] 'ghn23r'
'wr' [ 1545672] 'ghtyu1'
'AW' [ 142452] [ NaN]
'AW' [ 154522] [ NaN]
'AW' [ 1545242] [ NaN]
'AW' [ 154562] 'derf'
'AW' [ 15482] 'wedgrdt'
'AW' [ 1592] 'ftervd'
'JI' [ 15972] 'lofwfr'
'JI' [ 1952] [ NaN']
'JI' [ 1529] [ NaN']
'JI' [ 1592] [ NaN']
'JI' [ 151442] 'ftth'
'JI' [ 55152] 'eswq'
'JI' [ 8152] 'derf' };
I want to replace all the text information that corresponds ONLY to the country 'Aw', that is,
AW={'derf'
'wedgrdt'
'ftervd'
}
by some other text information say
AWnew={'AW3'
'AW2'
'AW5'
};
The only problem is that some of the elements in AW belong also to other countries.
For instance the element 'derf' appears not only in 'Aw' but also in countries 'wr' and 'JI'
So if I use
Matrix1=Matrix;
AW={'derf', 'wedgrdt', 'ftervd', 'lofwfr'}
AWnew={'AW3', 'AW2', 'AW5','AW8'};
for k=1:numel(AW)
Matrix(find(strcmp(Matrix,AW{k})))={AWnew{k}}
end
M=[Matrix1 Matrix(:,3)]
I get this result
M =
'country' 'values1' 'text' 'text'
'wr' [ 152] [ NaN] [ NaN]
'wr' [ 45152] [ NaN] [ NaN]
'wr' [ 654152] [ NaN] [ NaN]
'wr' [ 15552] 'derf' 'AW3'
'wr' [ 155682] 'dggyyd' 'dggyyd'
'wr' [15445352] 'ghn23r' 'ghn23r'
'wr' [ 1545672] 'ghtyu1' 'ghtyu1'
'AW' [ 142452] [ NaN] [ NaN]
'AW' [ 154522] [ NaN] [ NaN]
'AW' [ 1545242] [ NaN] [ NaN]
'AW' [ 154562] 'derf' 'AW3'
'AW' [ 15482] 'wedgrdt' 'AW2'
'AW' [ 1592] 'ftervd' 'AW5'
'JI' [ 15972] 'lofwfr' 'AW8'
'JI' [ 1952] [ NaN] [ NaN]
'JI' [ 1529] [ NaN] [ NaN]
'JI' [ 1592] [ NaN] [ NaN]
'JI' [ 151442] 'ftth' 'ftth'
'JI' [ 55152] 'eswq' 'eswq'
'JI' [ 8152] 'derf' 'AW3'
where as you can see this code has changed also the text information in other countries since the element 'AW3' appears now in all countries. But I want it to appear only in 'AW'
Similarly, I want to replace all the text information that corresponds ONLY to the country 'wr' by some other text information and again I want to replace all the text informatin that corresponds ONLY to the country 'JI' by some other textinformation
I have 300 such countries.
Is there a way to do that in matlab?
thanks
0 Kommentare
Antworten (1)
Jan
am 6 Feb. 2013
Bearbeitet: Jan
am 6 Feb. 2013
Why does 'lofwfr' belong to AW, although there is JI in the first column? Without this detail the answer would be easy: Simply concentrate on the lines, which starts with 'AW':
isAW = strcmp(Matrix(:, 1), 'AW');
new = cell(size(Matrix, 1), 1); % Pre-allocate!
AWtext = Matrix(isAW, 3); % Only the AW rows
for k = 1:numel(AW)
AWtext(strcmp(AWtext, AW{k})) = {AWnew{k}};
end
new(isAW) = AWtext;
Diese Frage ist geschlossen.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!