Replacing string variables that satisfy some criteria by other string variables

Dear all,
This is my matrix
Matrix={
'country' 'values1' 'text'
'wr' [ 152] [ NaN]
'wr' [ 45152] [ NaN]
'wr' [ 654152] [ NaN]
'wr' [ 15552] 'dedfr1'
'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] 'qwsd 23'
}
I want to replace all the text informatin that corresponds to 'Aw', that is,
AW={'derf'
'wedgrdt'
'ftervd'
'lofwfr'}
by other text information
AWnew={'AW3', 'AW2', 'AW5','AW8'};
so as to have a new matrix
Matrix={
'country' 'values1' 'text'
'wr' [ 152] [ NaN]
'wr' [ 45152] [ NaN]
'wr' [ 654152] [ NaN]
'wr' [ 15552] 'dedfr1'
'wr' [ 155682] 'dggyyd'
'wr' [ 15445352] 'ghn23r'
'wr' [ 1545672] 'ghtyu1'
'AW' [ 142452] [ NaN]
'AW' [ 154522] [ NaN]
'AW' [ 1545242] [ NaN]
'AW' [ 154562] 'AW3'
'AW' [ 15482] 'AW2'
'AW' [ 1592] 'AW5'
'JI' [ 15972] 'AW8'
'JI' [ 1952] [ NaN']
'JI' [ 1529] [ NaN']
'JI' [ 1592] [ NaN']
'JI' [ 151442] 'ftth'
'JI' [ 55152] 'eswq'
'JI' [ 8152] 'qwsd 23'
}
Similarly, I wan to replace the text information that corresponds to 'Wr' by other text information and so on..
I tried the following code only for 'AW'
list={'wr','AW' 'JI'}
for rr=2
countryName=list(rr); % which country we are referring t
Countryindic = cellfun(@(country)strcmp(country, countryName), Matrix(:,1)) ;
kkk2= find( Countryindic==1);
for k=1:numel(AW)
Matrix(kkk2,3)=cellfun(@(x) regexprep(x,AW{k},AWnew{k}),Matrix(kkk2,3),'un',0);
end
end
but I get this message
Undefined function or method 'regexprep' for input arguments of type 'double'.
Error in ==> @(x) regexprep(x,AW{k},AWnew{k})
Is there any other code that could get this job done efficienty or quickly?
My real matrix is 40000 by 29
thanks

 采纳的回答

AW={'derf','wedgrdt','ftervd','lofwfr'}
AWnew={'AW3', 'AW2', 'AW5','AW8'};
for k=1:numel(AW)
Matrix(find(strcmp(Matrix,AW{k})))={AWnew{k}}
end

4 个评论

thanks Azzi. I would like to ask the following question, if may. If I wanted to maintain column 3 instead of overwriting it and move the new column to the end of the matrix "Matrix" how would the code change?
thanks
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)]
%or simpler
col3=Matrix(:,3);
AW={'derf', 'wedgrdt','ftervd','lofwfr'}
AWnew={'AW3', 'AW2', 'AW5','AW8'};
for k=1:numel(AW)
col3(find(strcmp(col3,AW{k})))={AWnew{k}}
end
M=[Matrix col3]

请先登录,再进行评论。

更多回答(1 个)

looping over strrep would do, I presume:
A = {...}
Anew = {...}
for k = 1: ...,
Matrix(:,3) = strrep(Matrix(:,3), A{k},Anew{k})
end

类别

帮助中心File Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by