matlab:在一个文件中分离数据集
我有一个包含任意数量独立数据集的数据文件;数据分为三列,包含任意数量的行,将在MATLAB中进行处理。第一列包含时间值,另外两列包含相应的数据。这三个集合是串联的,每个集合不一定包含相同数量的行,时间值也不一定在相同的时间开始或结束。作为一个例子,考虑以下三个数据集的矩阵,由最左边列中的时间范围确定:matlab:在一个文件中分离数据集,matlab,dataset,Matlab,Dataset,我有一个包含任意数量独立数据集的数据文件;数据分为三列,包含任意数量的行,将在MATLAB中进行处理。第一列包含时间值,另外两列包含相应的数据。这三个集合是串联的,每个集合不一定包含相同数量的行,时间值也不一定在相同的时间开始或结束。作为一个例子,考虑以下三个数据集的矩阵,由最左边列中的时间范围确定: 0.010 xxx xxx 0.012 xxx xxx 0.014 xxx xxx 0.008 xxx xxx 0.011 xxx xx
0.010 xxx xxx
0.012 xxx xxx
0.014 xxx xxx
0.008 xxx xxx
0.011 xxx xxx
0.013 xxx xxx
0.014 xxx xxx
0.016 xxx xxx
0.009 xxx xxx
0.010 xxx xxx
0.012 xxx xxx
0.015 xxx xxx
其中xxx是对本练习不重要的数据值,但它们必须与最左边列中的相应时间值保持关联。在MATLAB中,将每个数据集与其他数据集分离的最简单/最有效的方法是什么?也就是说,我希望每个集合都包含在一个单独的变量中:
var1
0.010 xxx xxx
0.012 xxx xxx
0.014 xxx xxx
var2
0.008 xxx xxx
0.011 xxx xxx
0.013 xxx xxx
0.014 xxx xxx
0.016 xxx xxx
var3
0.009 xxx xxx
0.010 xxx xxx
0.012 xxx xxx
0.015 xxx xxx
首先,您实际上不需要单独的变量。您需要单元格数组,如var{1}、var{2}、var{3},或由变量var{i}索引 其次,在我看来,分离数据集的标准是从一行到下一行的时间步长为负。准确吗?如果是这样,那么diff将帮助您解决问题。要查找边值,请使用类似以下内容:
edges = find(diff(x(:,1)) < 0);
我把剩下的代码留给你……我来试试:
%# read the data
[a,b,c] = textread('data.txt', '%f%s%s');
%# find proper indices to the groups of data
inds = [
1 %# include first entry
find(diff(a)<0)+1 %# add one because diff shrinks the array by 1
length(a)]; %# include the last entry
%# simply loop over the indices, and assign each range thus defined
%# to an entry in a cell array
A = cell(numel(inds)-1,1);
B = A;
C = A;
for ii = 1:numel(inds)-1
range = inds(ii) : inds(ii+1)-1;
A{ii} = a(range);
B{ii} = b(range);
C{ii} = c(range);
end
这个问题基本上是,如何从矩阵中选择行?并充分说明了它以及有关索引的Matlab文档。这个问题应该改写,使之更加明确。
%# read the data
[a,b,c] = textread('data.txt', '%f%s%s');
%# find proper indices to the groups of data
inds = [
1 %# include first entry
find(diff(a)<0)+1 %# add one because diff shrinks the array by 1
length(a)]; %# include the last entry
%# simply loop over the indices, and assign each range thus defined
%# to an entry in a cell array
A = cell(numel(inds)-1,1);
B = A;
C = A;
for ii = 1:numel(inds)-1
range = inds(ii) : inds(ii+1)-1;
A{ii} = a(range);
B{ii} = b(range);
C{ii} = c(range);
end