0

我有一个大型3D CELL ARRAY (x1),我必须删除空行。我怎样才能做到这一点?

我的单元格数组示例(变量的某些部分):

val(:,:,1) = 

[20]    []    []    []    []    []    []    []
[ 0]    []    []    []    []    []    []    []
[ 0]    []    []    []    []    []    []    []
[]      []    []    []    []    []    []    []

(...)

val(:,:,42) = 


[ 34225]    [   215]    [    0]    [   0]    [    0]    [    0]    [    0]    [    0]
[ 85200]    [   545]    [    0]    [   0]    [    0]    [    0]    [    0]    [    0]
[ 65074]    [   190]    [ 1000]    [   0]    [    0]    [    0]    [    0]    [    0]
[ 81570]    [  1385]    [ 2475]    [   0]    [    0]    [    0]    [    0]    [    0]    
[ 67236]    [   530]    [  365]    [   0]    [    0]    [    0]    [    0]    [    0]
[ 61338]    [     0]    [  100]    [   0]    [    0]    [    0]    [    0]    [    0]
[]          []          []         []        []         []         []         []    
[]          []          []         []        []         []         []         []  
[]          []          []         []        []         []         []         []   

在这种情况下,我想从这些变量中排除第 4 行(:,:,1)、最后三行以及所有其他行。(:,:,42)

我试过了

x1(all(all(cellfun((@isempty,x1),2),:,:) = [];

但它给了我以下错误:

错误:表达式或语句不正确——可能是不平衡的 (、{ 或 [.

PS:我不能使用"==",因为它是一个单元阵列。

提前致谢

4

3 回答 3

2

这里似乎有两个问题。一是您使用的是 3D 元胞数组,并且您似乎想从不同的平面删除不同数量的行。这会给你一些不能形成正确 MxNxP 结构的东西,因为 M 不会相同。

也就是说,我可以告诉您如何删除二维元胞数组中所有为空的行。假设val是 MxN。然后

val2 = val(~all(cellfun(@numel,val)==0,2),:);

如果您想使用您描述的 3D 数据,您必须将每个平面的结果分别存储在一个单元格中。像这样的东西:

val2 = cell(1,1,size(val,3));
for i = 1:size(val,3)
    valplane = val(:,:,i);
    val2{i} = valplane(~all(cellfun(@numel,valplane)==0,2),:);
end
于 2013-04-23T03:54:35.977 回答
0

这应该适用于您的特定行,例如第一行:

i=1;    %Row number
val(~all(cellfun(@isempty,val(:,:,i)')),:,i);

例如:

>> 
val = cell(4,8,2);
val(:,:,1) = {
[20]    []    []    []    []    []    []    []
[ 0]    []    []    []    []    []    []    []
[ 0]    []    []    []    []    []    []    []
[]      []    []    []    []    []    []    []
};
>> 
i=1;    %Row number
val(~all(cellfun(@isempty,val(:,:,i)')),:,i)

结果是:

ans = 

    [20]    []    []    []    []    []    []    []
    [ 0]    []    []    []    []    []    []    []
    [ 0]    []    []    []    []    []    []    []
于 2013-04-23T04:17:51.713 回答
0

无法从 3D 阵列中移除不同的行,您可以移除“地板”、层或“墙壁”。(如果你想象一个逐层的建筑物,我想要一个垂直且垂直的平面到你的视线,而墙壁是沿着你的视线的垂直平面)。

不应该以这种方式存储数字数据,即每个单元格一个数字。当双精度标量只有 8 个字节时,每个单元会产生 112 个字节的开销。

根据我可以从您的示例中观察到的稀疏模式,您可能会通过将其转换为稀疏数组而受益,该数组仍然引用数组的原始大小但不存储0or []。但是,稀疏数组只是二维的,其中附加层(来自第三维)沿第二维连续存储。

您的示例的简化版本将阐明:

val(:,:,1) = {
[20]    []    []    []
[ 0]    []    []    []
[ 0]    []    []    []
[]      []    []    []};
val(:,:,2) = {
[ 34225]    [   215]    [    0]    [   0]    
[ 85200]    [   545]    [    0]    [   0]   
[ 65074]    [   190]    [ 1000]    [   0]   
[]          []          []         []     };

% Find non-zero empty elements (0s will be discarded automatically)
[r,c] = find(~cellfun('isempty',val));

% Convert to sparse (note the coordinates are only 2D)
sp    = sparse(r,c,[val{:}]);
sp =
   (1,1)             20
   (1,5)          34225
   (2,5)          85200
   (3,5)          65074
   (1,6)            215
   (2,6)            545
   (3,6)            190
   (3,7)           1000

% Convert to full to see what happened with the 3rd dimension
full(sp)
ans =
     20      0      0      0  34225   215     0     0     
      0      0      0      0  65074   190     0     0
      0      0      0      0  85200   545  1000     0

您可以看到第二层已水平连接(沿列)。如果需要恢复 3D,可以使用 reshape。

valvs的优势在于sp减少了存储:

  Name      Size             Bytes  Class     Attributes
  sp        3x8                200  double    sparse    
  val       4x4x2             3704  cell
于 2013-04-23T14:15:39.087 回答