Linear index for cell array with 2 subscripts

2 次查看(过去 30 天)
Hi,
I want to delete the empty cells in a cell array with 2 subscripts, for example A. One approach is
[r,v] = find(cellfun('isempty',A)==1);
r= unique(r);
A(r,:) = [];
However, is it possible to use sub2ind() to more specifically delete the empty cells in A? I have tried this way but it does not produce desired results
[r,v] = find(cellfun('isempty',A)==1);
idx = sub2ind(size(A),r,v);
A(idx) = [];

回答(1 个)

the cyclist
the cyclist 2015-3-13
I think you accidentally deleted the non-empty cells.
Also, I am not sure what it means to delete an empty cell. It is already empty. If you want to keep the shape of A intact, then something has to be there.
This code will delete the empty cells, but return a one-dimensional cell array:
A = {[1 2],[3 4], [];
[],[5 6], [7 8]}
[r,v] = find(~cellfun('isempty',A))
idx = sub2ind(size(A),r,v);
A = A(idx)
  2 个评论
Hongtao Li
Hongtao Li 2015-3-13
编辑:Hongtao Li 2015-3-13
Hi, the cyclist
Thanks for your answers. Actually that is the problem I encountered. I want to keep the structure of the cell array, i.e. it is still with 2 subscripts.
the cyclist
the cyclist 2015-3-13
编辑:the cyclist 2015-3-13
OK. But what do you want instead of an empty cell? For example, take a look at the array A that I created in my answer. It is a 2x3 cell array, with two empty cells.
What do you want the output to be? If you still want a 2x3 cell array, then element (1,3) has to be something. As I asked before, what you do mean by "delete"?
Alternatively, what are you trying to do with array A that has a problem because of the empty cells?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by