Linear index for cell array with 2 subscripts

조회 수: 1 (최근 30일)
Hongtao Li
Hongtao Li 2015년 3월 13일
편집: the cyclist 2015년 3월 13일
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에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by