Linear index for cell array with 2 subscripts
2 views (last 30 days)
Show older comments
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) = [];
0 Comments
Answers (1)
the cyclist
on 13 Mar 2015
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 Comments
the cyclist
on 13 Mar 2015
Edited: the cyclist
on 13 Mar 2015
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?
See Also
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!