Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 循环结构并找到相关性_Matlab_Structure_Correlation - Fatal编程技术网

Matlab 循环结构并找到相关性

Matlab 循环结构并找到相关性,matlab,structure,correlation,Matlab,Structure,Correlation,下面的示例类似于我正在处理的一个类似问题,尽管下面的代码只是一个示例,但它的结构与我的实际数据集的格式相同 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(32

下面的示例类似于我正在处理的一个类似问题,尽管下面的代码只是一个示例,但它的结构与我的实际数据集的格式相同

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个位置的成对空气温度之间的相关性。我正在寻找一种方法来研究“SolRad”和“Rain”之间的相关性(与AirT的过程相同)或结构中表示的任何变量。我可以通过将输入替换为“数据”来实现这一点,但这似乎相当冗长,尤其是当涉及许多不同的变量时。有什么办法吗?我曾经尝试过使用循环,但似乎比我想象的更难尝试将数据转换为与示例相同的格式

让我们看看这是否有帮助,或者你是这么想的:

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

如果我误解了,或者这比你想的要复杂,请告诉我。谢谢

让我们看看这是否有帮助,或者你是这么想的:

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
如果我误解了,或者这比你想的要复杂,请告诉我。谢谢

你是你的朋友

我建议创建一个结构,其中“name”是一个字段,其他字段是您想要的任何字段。无论您如何设置
结构
,都可以使用
fn=字段名
返回字段的单元格数组。通过在包含名称的变量周围使用括号,可以使用这些名称访问结构的内容

fn = fieldnames(s);
for i=1:length(fn)
disp([fn{i} ':' s.(fn{i})]
end
当然,你对这些价值观做什么都取决于你自己

你是你的朋友

我建议创建一个结构,其中“name”是一个字段,其他字段是您想要的任何字段。无论您如何设置
结构
,都可以使用
fn=字段名
返回字段的单元格数组。通过在包含名称的变量周围使用括号,可以使用这些名称访问结构的内容

fn = fieldnames(s);
for i=1:length(fn)
disp([fn{i} ':' s.(fn{i})]
end
当然,你对这些价值观做什么都取决于你自己