Removing empty cells with non-zero dimensions

조회 수: 15 (최근 30일)
AS
AS 2020년 8월 24일
편집: Bruno Luong 2020년 8월 25일
My code needs to deal with a cell array X, each cell of which is itself a cell array, containing a double array. For example, X could look as follows:
X = cell(N,1);
for i=1:N
X{i}=cell(1,10);
for j=1:10
X{i}{j} = randi(10, 5,2); %each cell contains a double array of size (5,2)
end
end
While manipulating my code, some rows of these double arrays might get removed. For example:
for i=1:N
for j=1:10
X{i}{j}(X{i}{j}(:,1) < 3,:) = [];
end
end
In some cases, all elements of some double arrays get removed, resulting in a 0×2 empty double matrix. This nonzero size is causing problems elsewhere in my code, how do I efficiently replace these with empty arrays?
My current approach is to call the following forloop after each set of manipulatoins that might result in empty arrays with nonzero size.
for i=1:N
for j=1:10
if isempty(X{i}{j})
X{i}{j} = [];
end
end
end
However, I'm fairly certain that there is no better way of doing this. Any suggestions?
Edit: I want to emphasize that I do not want to remove the empty cells. What I do want is to replace any 0x2 empty double matrices with 0x0 matrices.
The 10 cells inside each X{i} represent "physical" lattice sites in my simulation. An empty cell does have a meaning, and should not be removed.
  댓글 수: 3
AS
AS 2020년 8월 24일
No, I explicitly want to keep the empty cells, I just don't want them to have a non-zero size if they are empty.
The 10 cells inside each X{i} represent "physical" lattice sites in my simulation. An empty cell does have a meaning, and should not be removed.
Adam Danz
Adam Danz 2020년 8월 24일
편집: Adam Danz 2020년 8월 24일
I see. I'll update my answer.
Note that the isempty function will return the same results whether the cell is 0xn, nx0 or 0x0 but if you're using the cell size for any reason, then it matters what the empty dimensions are.

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2020년 8월 24일
편집: Adam Danz 2020년 8월 24일
How to remove empty cells
To remove all empty cells in the 2nd layer of a nested cell array named X,
for i = 1:numel(X)
X{i}(cellfun(@isempty,X{i})) = [];
end
Or, in 1 line,
X = cellfun(@(C){C(~cellfun(@isempty,C))},X);
That may eliminimate all of the 2nd layer of nested cells in which case some of the first layer may become empty. If you'd like to eliminate them as well (ie, all cells where all nested cells were removed),
X(cellfun(@isempty, X)) = [];
How to replace 0xn or nx0 empty cells with 0x0
To replace all 0xn or nx0 cells in the 2nd layer of a nested cell array named X,
for i = 1:numel(X)
X{i}(cellfun(@isempty,X{i})) = {[]};
end
  댓글 수: 1
Adam Danz
Adam Danz 2020년 8월 25일
편집: Adam Danz 2020년 8월 25일
I'm guessing that your workflow uses size() which is why it's a problem when a cell is 0x2. If that's the case, you could avoid this entire process if you use isempty() within your workflow instead of size(). If the size of the arrays are already stored somewhere as sz, you could use something like if any(sz==0).
Also, if the second block of code in your question resembles what you're actually doing, you could shave off some time by fixing the problem within that section rather than additing another set of loops to convert 0x2 to 0x0. This is the fastest method yet, I believe (not that it matters at this point).
% Replace the 2nd block of code in your question with this
for i=1:N
Xi = X{i};
for j=1:10
rmIdx = Xi{j}(:,1) < 3;
if all(rmIdx)
Xi{j} = [];
else
Xi{j}(rmIdx,:) = [];
end
end
X{i} = Xi;
end

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bruno Luong
Bruno Luong 2020년 8월 24일
편집: Bruno Luong 2020년 8월 24일
I like your for-loop; you might speed up a little bit
for i=1:N
Xi = X{i};
Xi(cellfun('isempty',Xi)) = {[]}; % switch to string from Rik's remark
X{i} = Xi;
end
  댓글 수: 13
AS
AS 2020년 8월 24일
편집: AS 2020년 8월 24일
@Bruno Luong, Would you mind explaning why defining and then using Xi = X{i}; inside the first loop speeds things up? It's more than twice as fast on my machine.
Bruno Luong
Bruno Luong 2020년 8월 25일
편집: Bruno Luong 2020년 8월 25일
Well very simple explanation:
with X{i}{j} you tells matlab to indexing twice with i variable then with j.
With Xi{j} only one indexing once with j since Xi is a variable. In the for-loop it makes a difference.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by