Deleting empty rows in a 3D Cell Array in MatLab

I have a large 3D CELL ARRAY (x1) which I have to delete the empty rows. How can I do this?
Example of my cell array (some pieces of the variable):
val(:,:,1) =
[20] [] [] [] [] []
[ 0] [] [] [] [] []
[ 0] [] [] [] [] []
[] [] [] [] [] []
(...)
val(:,:,42) =
[ 34225] [ 215] [ 0] [ 0] [ 0] [ 0]
[ 85200] [ 545] [ 0] [ 0] [ 0] [ 0]
[ 65074] [ 190] [ 1000] [ 0] [ 0] [ 0]
[ 81570] [ 1385] [ 2475] [ 0] [ 0] [ 0]
[ 67236] [ 530] [ 365] [ 0] [ 0] [ 0]
[ 61338] [ 0] [ 100] [ 0] [ 0] [ 0]
[] [] [] [] [] []
[] [] [] [] [] []
[] [] [] [] [] []
In this case, I want to exclude the 4th row of `(:,:,1)`, the three last rows from `(:,:,42)` and all the others from these variable.
I've tried
x1(all(all(cellfun((@isempty,x1),2),:,:) = [];
But it gave me this following error:
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
PS: I cannot use `"=="` because its a cell array.
Thanks in advance

2 个评论

You cannot erase the 4th row of val(:,:,1) without erasing as well the 4th row for all valid 3rd indices. In your example, doing this would erase val(4,:,42) as well.. is it what you want to do?
Also, your example should show that val(:,:,1) and val(:,:,42) have the same size (for val to be a 3D cell array). Are you sure that you don't have a cell array of 2D cell arrays? (you index val as a 3D cell array though)
Wow, thats bad, because I want to erase ONLY the empty rows. Yeah, they have the same size, coincidently ! I'm sure I got a 3D Cell Array !

请先登录,再进行评论。

 采纳的回答

If you want to be able to remove rows from single "pages", you should use a cell array of cell arrays, e.g.
A = cell(3, 1) ;
A{1} = {2, 3, 4; [], [], []; [], 5, []} ;
A{2} = {9} ;
A{3} = {2, 3, 4, 5, 6; [], 9, [], [], []; ...
[], [], [], [], []; [], [], [], [], []} ;
for k = 1 : length(A)
id = all(cellfun(@isempty, A{k}), 2) ;
A{k}(id,:) = [] ;
end
After execution, we get:
>> A{1}
ans =
[2] [3] [4]
[] [5] []
>> A{2}
ans =
[9]
>> A{3}
ans =
[2] [3] [4] [5] [6]
[] [9] [] [] []
and you can see that the content of cells of A doesn't need to match in size.

更多回答(1 个)

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by