2012-03-10 76 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];