Linear index for cell array with 2 subscripts

2 visualizzazioni (ultimi 30 giorni)
Hongtao Li
Hongtao Li il 13 Mar 2015
Modificato: the cyclist il 13 Mar 2015
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) = [];

Risposte (1)

the cyclist
the cyclist il 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 Commenti
Hongtao Li
Hongtao Li il 13 Mar 2015
Modificato: Hongtao Li il 13 Mar 2015
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 il 13 Mar 2015
Modificato: the cyclist il 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?

Accedi per commentare.

Categorie

Scopri di più su Multidimensional Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by