How to delete specific cells according to the condition from cell array?

I have a cell array test (4x189).
Each cell is a double array of different sizes. I want to delete all the cells, that look like this (basically, they are all 7x4, the same looking, but different numbers):
NaN 1 0.437500000000000 0
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN 8 NaN NaN
NaN 719 NaN NaN
NaN 719 NaN NaN
NaN NaN NaN NaN
Could you, please, help me to do that?
Thank you so much!

3 Comments

You have not yet given us enough information to figure out which elements to keep.
What is the defining characteristic that differentiates the arrays you want to keep, from the arrays you do not?
Did you mean that all "bad" arrays are 7x4, and all "good" arrays are a different size?
Or are some of the "good" arrays also 7x4, but do not have NaN?
Also, suppose you "delete" only the upper-left element. What do you mean by that? The cell array will still need to be 4x189, so how do you "delete" it? Replace that element with an empty array?
Did you mean that all "bad" arrays are 7x4, and all "good" arrays are a different size?
Yes, that's correct! And they also have something else in common: B6 = B7. (values in second column and 6th and 7th row are equal, here 719=719).
I would like to "move" the next cell, to this place, and add the empty array at the end to keep the same size of cell array.
Basically, I am trying to solve the problem of removing sheets from Excel file that satisfy specific condition (B6=B7).

Sign in to comment.

 Accepted Answer

This line of code will identify the cell locations that have a 7x4 array, having those two elements equal:
deleteIdx = cellfun(@(x)(x(5,2)==x(6,2))&&all(size(x)==[7,4]),test);
It's still not perfectly clear to me what you prefer as output, but maybe this is good enough.

6 Comments

Thank you!
It gives me the logical array, where 1 means the cell I want to remove (true for the condition). How do I remove these cells from my test array so, that just next cell after removed one, moves to the deleted position?
(Example: cell in the position 1st row, 3d column is removed, so the next cell (from the 4th column) just will move to the third column istead of deleted one.)
I think the following does what you want:
deleteIdx = cellfun(@(x)(x(5,2)==x(6,2))&&all(size(x)==[7,4]),test);
keepIdx = not(deleteIdx); % Could have defined keepIdx similarly to the way deleteIdx was done, but I am being lazy.
% Initialize the output as a cell array with all empty cells.
output = cell(size(test));
% Fill the output sequentially (down the columns), from only the valid
% cells.
output(1:sum(keepIdx(:))) = test(keepIdx);
Better version of the above, just defining the index of the cell elements to keep:
keepIdx = cellfun(@(x)(x(5,2)~=x(6,2))||any(size(x)~=[7,4]),test);
output = cell(size(test));
output(1:sum(keepIdx(:))) = test(keepIdx)
You might want to double-check that I coded the keep condition accurately.
I see that you want to fill along rows, rather than columns. I think it's easiest to just transpose the original matrix before doing the above, and then transpose the output back.
output(1:sum(keepIdx(:))) = test(keepIdx)
Unfortunately, this one is not saving the structure of cell array test.
If my array test is 4 by 189, I still would like to have a cell array with 4 rows, but less columns (let's say if in the first row I removed 2 columns, in the second row - 10 columns, in the third - 50 columns and in the forth - nothing, as a result I want to get an array of 4 by 189, but in the first row it will be 187 not empty elements and 2 NAN, in the third row - already 149 not empty elements).
Did you do the preallocation step?
output = cell(size(test));
Also, you said you wanted empty cells, but now you say you want NaN. Which is it?
Can you upload your 4x189 array, to work on directly?

Sign in to comment.

More Answers (1)

If you want to keep the output as a cell array (as the input), you cannot delete them, but you can assign them to empty array.
for j=1:189
for i=1:4
if all(size(test{i,j})==[7 4]) && test{i,j}(5,2)==test{i,j}(6,2)
test{i,j} = [];
end
end
end

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!