필터 지우기
필터 지우기

Replacing string variables that satisfy some criteria by other string variables

조회 수: 2 (최근 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
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by