Replacing string variables that satisfy some criteria by other string variables

1 回表示 (過去 30 日間)
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

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 4 日
編集済み: Azzi Abdelmalek 2013 年 2 月 4 日
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 件のコメント
tzaloupas
tzaloupas 2013 年 2 月 4 日
Perfect! thanks a lot!!!!!
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 4 日
編集済み: Azzi Abdelmalek 2013 年 2 月 4 日
%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 件)

Jos (10584)
Jos (10584) 2013 年 2 月 4 日
looping over strrep would do, I presume:
A = {...}
Anew = {...}
for k = 1: ...,
Matrix(:,3) = strrep(Matrix(:,3), A{k},Anew{k})
end

カテゴリ

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