2012-02-27 18 views
1

次の例は、私が扱っている同様の問題に似ていますが、下のコードは単なる例であり、実際のデータセットと同じ形式で構成されています。ループスルー構造と相関関係の検索

clear all 

England = struct('AirT',rand(320,1),'SolRad',rand(320,1),'Rain',rand(320,1)); 
Wales = struct('AirT',rand(320,1),'SolRad',rand(320,1),'Rain',rand(320,1)); 
Ireland = struct('AirT',rand(320,1),'SolRad',rand(320,1),'Rain',rand(320,1)); 
Scotland = struct('AirT',rand(320,1),'SolRad',rand(320,1),'Rain',rand(320,1)); 

Location = struct('England',England,'Wales', Wales, 'Ireland',Ireland,'Scotland',Scotland); 

FieldName={'England','Wales','Scotland','Ireland'}; 
Data = {England.AirT,Wales.AirT,Scotland.AirT,Ireland.AirT}; 
Data = [FieldName;Data]; 
Data = struct(Data{:}); 
Data = cell2mat(struct2cell(Data)'); 
[R,P] = corrcoef(Data,'rows','pairwise'); 
R_Value= [FieldName(nchoosek(1:size(R,1),2)) num2cell(nonzeros(tril(R,-1)))]; 

したがって、このスクリプトは、4つの場所の気温のペア間の相関を示します。私は場所(AirTと同じプロセス)または構造内に示されている任意の変数間の 'SolRad'と 'Rain'の相関を調べる方法を探しています。私は入力を 'データ'に置き換えることでこれを行うことができましたが、これは特に多くの異なる変数を含むときにはむしろ長く巻き込まれているようです。これを行う方法に関するアイデア?私はループを使用しようとしましたが、私は試して、例と同じ形式にデータを取得するよりも難しいようです。

答えて

1

このことができます、またはあなたが何を考えているのであれば見てみましょう:

clear all 

England = struct('AirT',rand(320,1),'SolRad',rand(320,1),'Rain',rand(320,1)); 
Wales = struct('AirT',rand(320,1),'SolRad',rand(320,1),'Rain',rand(320,1)); 
Ireland = struct('AirT',rand(320,1),'SolRad',rand(320,1),'Rain',rand(320,1)); 
Scotland = struct('AirT',rand(320,1),'SolRad',rand(320,1),'Rain',rand(320,1)); 

Location = struct('England',England,'Wales', Wales, 'Ireland',Ireland,'Scotland',Scotland); 

% get all the location fields 
FieldName = transpose(fieldnames(Location)); 
% get the variables recorded at the first location 
CorrData = fieldnames(Location.(FieldName{1})); 
% get variables which were stored at all locations(just to be safe, 
% we know that they are all the same) 
for ii=2:length(FieldName) 
    CorrData = intersect(CorrData,fieldnames(Location.(FieldName{ii}))); 
end 

% process each variable that was recorded 
for ii=1:length(CorrData) 
    Data = cell(1,length(FieldName)); 
    % get the variable data from each location and store in Data 
    for jj=1:length(FieldName) 
    Data{jj} = Location.(FieldName{jj}).(CorrData{ii}); 
    end 
    % process the data 
    Data = [FieldName;Data]; 
    Data = struct(Data{:}); 
    Data = cell2mat(struct2cell(Data)'); 
    [R,P] = corrcoef(Data,'rows','pairwise'); 
    R_Value= [FieldName(nchoosek(1:size(R,1),2)) num2cell(nonzeros(tril(R,-1)))]; 
    % display the data, sounds good right? 
    fprintf(1,'Correlation for %s\n',CorrData{ii}); 
    for jj=1:size(R_Value,1) 
    fprintf(1,'%s\t%s\t%f\n',R_Value{jj,1},R_Value{jj,2},R_Value{jj,3}); 
    end 
end 

は、私が誤解なら、私に知らせて、またはこれはあなたが考えていたものよりも複雑である場合。ありがとう!

+0

私がやろうとしていたことはまさに、ありがとうございました。 – Emma

0

fieldnames(s)とdynamic field referencesはあなたの友人です。

私が示唆しているのは、「名前」がフィールドであり、他のフィールドがあなたの好きなものであるという構造を作ることです。 structure sの設定方法にかかわらず、fn = fieldnames(s);を使用してフィールドのセル配列を返すことができます。これらの名前を使用して構造体の内容にアクセスするには、名前を含む変数のまわりのカッコを使用します。

fn = fieldnames(s); 
for i=1:length(fn) 
disp([fn{i} ':' s.(fn{i})] 
end 

あなたが値を使って行うことは、もちろんあなた次第です!