Info

此问题已关闭。 请重新打开它进行编辑或回答。

Replacing string variables that satisfy some criteria by some other string variables in a cell vector when there are repetitions

1 次查看(过去 30 天)
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

回答(1 个)

Jan
Jan 2013-2-6
编辑:Jan 2013-2-6
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;
  3 个评论
Jan
Jan 2013-2-6
编辑:Jan 2013-2-6
Then avoid running this line twice:
new = cell(size(Matrix, 1), 1); % Pre-allocate!
I do not understand what the IF statement could be useful for. And I do not understand, why 'lofwfr' belongs to 'AW' in your example.
tzaloupas
tzaloupas 2013-2-6
Thanks Jan.
I corrected the mistake so as now 'lofwfr' does not belongs to 'AW'.
I use the If statement because I upload various excel files successively one at a time and some of these files contain a different number of countries.
Regarding what you said to avoid running this line twice:
new = cell(size(Matrix, 1), 1); % Pre-allocate!
I still can not find the solution.
I tried something like
uniqueA = unique(Matrix(2:end,1));
new = cell(size(Matrix, 1), 1); % Pre-allocate!
if strcmp(uniqueA(1),'wr')
isAW = strcmp(Matrix(:, 1), 'wr');
AWtext = Matrix(isAW, 3); % Only the AW rows
for k = 1:numel(wr)
AWtext(strcmp(AWtext, wr{k})) = {wrnew{k}};
end
new(isAW) = AWtext;
end
if strcmp(uniqueA(1),'AW')
isAW = strcmp(Matrix(:, 1), 'AW');
AWtext = Matrix(isAW, 3); % Only the AW rows
for k = 1:numel(AW)
AWtext(strcmp(AWtext, AW{k})) = {AWnew{k}};
end
new(isAW) = AWtext;
end
M=[Matrix new]
could you please help me to modify you code?
thanks in advance

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by