2012-03-10 91 views
0

为了一个字符串,使这个问题更容易来形容我所提供的以下示例代码,这是类似我一起工作的实际数据:分裂在MATLAB

clear all 
AirT = {rand(32,1),rand(32,1),rand(32,1),rand(32,1)}; 
SolRad = {rand(32,1),rand(32,1),rand(32,1),rand(32,1)}; 
Rain = {rand(32,1),rand(32,1),rand(32,1),rand(32,1)}; 
Location = {'England','Wales','Scotland','Ireland'}; 
points = {'old','old','old','new'}; 
CorrVariables = {'AirT','SolRad','Rain'}; 
for i = 1:length(Location); 
    Data = @(location) struct('Location',location,CorrVariables{1},AirT{i},... 
     CorrVariables{2},SolRad{i},CorrVariables{3},Rain{i}); 
    D(i) = Data(Location{i}); 
end 
FieldName = {D.Location}; 
R = corrcoef([D.AirT],'rows','pairwise'); 
R_Value = [Location(nchoosek(1:size(R,1),2)) num2cell(nonzeros(tril(R,-1)))]; 
q = points(nchoosek(1:size(R,1),2)); 
    %to calculate the combination of these points we need to convert the 
    %cell into a matrix. 
Re = [R_Value q]; 

从这个例子,我想在Re第5列中创建另一个单元格数组,它依赖于第4列和第5列中的字符串。因此,如果Re中的第4列和第5列是相等的,例如'old''old',那么第6列应显示'old' 。但是,如果细胞不同,例如'old''new',那么我希望新的单元格数组(即Re中的第6列)陈述'old/new'。

这怎么可能?

回答

2

从你的描述,我认为最明显的方法是使用字符串连接正则表达式的组合。

首先结合4列和第5放入一个新列:

newColumn = strcat(Re(:,4), '/', Re(:,5)); 

现在查找重复图案,并与匹配的所述第一令牌替换:

newColumn = regexprep(newColumn, '(\w+)/\1', '$1'); 

合并到现有的单元矩阵:

Re = [Re, newColumn];