如果我错了,请纠正我,但听起来您的 3x15000 矩阵包含一些离散数据,您希望将每个数据块保存为单独的矩阵。
假设您的矩阵如下所示:
h(:,[2,3,5]) = rand(3,3)
h =
NaN 0.9649 0.9572 NaN 0.1419 NaN
NaN 0.1576 0.4854 NaN 0.4218 NaN
NaN 0.9706 0.8003 NaN 0.9157 NaN
现在您想将第 2,3 列复制到一个矩阵中,将第 5 列复制到另一个矩阵中。一种方法是首先找到仅包含 NaN 的列。你可以这样做:
ind = all(isnan(h),1)
ind =
1 0 0 1 0 1
isnan 返回一个由 1 和 0 组成的数组,其中 1 表示 NaN 所在的位置。 all(...,1) 返回所有行都是 NaN 的列索引。 ind 包含您想要的标志。要单独保存每个数据块,您可以使用简单的for 循环。这是一个快速而肮脏的解决方案:
j = 1;
k = 1;
x = nan(3,1); %temporary matrix to store numerical values
c = cell(2,1); %cell array to store the chunks of data individually
%if you can predict how many elements `c` should have, then
%you can pre-allocate appropriately.
for i=1:length(ind)
%loop through all the columns
if ind(i) == 1
%if we encounter a flag and 'x' has data, dump 'x' into 'c{k}',
%reset 'x' and continue.
if ~all(isnan(x))
c{k} = x;
k = k+1;
end
%reset x
x = nan(3,1);
j=1;
continue
end
x(:,j) = h(:,i);
j = j+1;
%catch data at the end, if last column of h does not contain all NaNs
if i==length(ind)
c{k} = x;
end
end
您的数据块以矩阵形式存储在元胞数组中:
c{1}
ans =
0.9649 0.9572
0.1576 0.4854
0.9706 0.8003
c{2}
ans =
0.1419
0.4218
0.9157
希望这会有所帮助。