필터 지우기
필터 지우기

changing the string "JF 2009" to two seperate strings "J 2009" " F2009"

조회 수: 3 (최근 30일)
Dear all I have the following cell matrix
A={
1 'SLO' ' '
1 'SLO' ' '
1 'SLO' 'JF 2009'
1 'SLO' 'MA 2009'
1 'SLO' 'MJ 2009'
1 'SLO' 'JA 2009'
1 'SLO' 'SO 2009'
1 'SLO' 'ND 2009'
1 'SLO' 'JF 2010'
1 'SLO' 'MA 2010'
1 'SLO' 'MJ 2010'
1 'SLO' 'JA 2010'
1 'SLO' 'SO 2010'
1 'SLO' 'ND 2010'
1 'SLO' 'JF 2011'
1 'SLO' 'MA 2011'
1 'SLO' 'MJ 2011'
1 'SLO' 'JA 2011'
2 'SLO' ' '
2 'SLO' ' '
2 'SLO' 'JF 2009'
2 'SLO' 'MA 2009'
2 'SLO' 'MJ 2009'
2 'SLO' 'JA 2009'
2 'SLO' 'SO 2009'
2 'SLO' 'ND 2009'
2 'SLO' 'JF 2010'
2 'SLO' 'MA 2010'
2 'SLO' 'MJ 2010'
2 'SLO' 'JA 2010'
2 'SLO' 'SO 2010'
2 'SLO' 'ND 2010'
2 'SLO' 'JF 2011'
2 'SLO' 'MA 2011'
2 'SLO' 'MJ 2011'
2 'SLO' 'JA 2011'
}
As you can see I have bimontly data. I want to modify the last column so as to have months like
Amodified={
1 'SLO' ' '
1 'SLO' ' '
1 'SLO' 'J 2009'
1 'SLO' 'F 2009'
1 'SLO' 'M 2009'
1 'SLO' 'A 2009'
1 'SLO' 'M 2009'
1 'SLO' 'J 2009'
1 'SLO' 'J 2010' }
and so forth
Is there any "clever code to do that?
NOte that the fist column represents the individual. In my real matrix I have 30000 individuals.
thanks in advance

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2012년 7월 25일
편집: Azzi Abdelmalek 2012년 7월 25일
% try this
n=size(A,1);
c1=num2cell([1:n]')
a=[c1 A]
b=sortrows(a,4)
B=b(:,4)
d=max(find(cellfun(@(x) length(x)>2,B)==0))
C=b(d+1:end,:)
n1=size(C,1)
ind1=1:2:2*n1-1;ind2=2:2:2*n1,C1=C;C2=C;
C1(:,4)=cellfun(@(x) strtrim(regexprep(x,x(1),'')),C(:,4),'UniformOutput',false)
C2(:,4)=cellfun(@(x) strtrim(regexprep(x,x(2),'')),C(:,4),'UniformOutput',false)
D=cell(n1*2,4); D(ind1,:)=C2; D(ind2,:)=C1;
E=sortrows([b(1:d,:) ; D],1);
Result=E(:,2:4)
  댓글 수: 2
salva
salva 2012년 7월 25일
thank you both!
salva
salva 2012년 7월 25일
편집: salva 2012년 7월 25일
the only problem though is this part
ind1=1:2:2*n1-1;ind2=2:2:2*n1
In my original matrix I have 300000 individuals!
HOw could I solve that problem?
thanks

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Wayne King
Wayne King 2012년 7월 25일
편집: Wayne King 2012년 7월 25일
One way:
Amodified = A;
B = char(A(:,3));
B(:,2) = [];
B = cellstr(B);
Amodified(:,3) = B;
  댓글 수: 4
salva
salva 2012년 7월 25일
thank you. I mean that since the dimension of column 3 will change we must also change the dimension of the other columns
Wayne King
Wayne King 2012년 7월 25일
It's a cell array so why do you have to adjust the "dimension" of the other columns, they aren't equal to begin with
size(A{1,1}), size(A{1,2})

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Display Point Clouds에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by