How do I delete slices in 3D-Array?
24 次查看(过去 30 天)
显示 更早的评论
Hey there,
I have a multidimensional Array where some of the slices along the third dimension only contains zeros in rows and columns. How do I delete those?
Thanks :)
2 个评论
Saravanan Sengottuvel
2021-4-8
Generating an example 3D array that has 10 slices
A = repmat([0 0 0 0; 1 0 0 0; 1 0 1 0],[1 1 10]);
Modifying the 4th & 8th slice to contain only zeros in rows and columns
A(:,:,4)=[0 0 0 0;0 0 0 0;0 0 0 0];
A(:,:,8)=[0 0 0 0;0 0 0 0;0 0 0 0];
Find the slice index in 3rd dimension that has only zeros in rows and columns
idx = all(A == 0, [1 2])
Now, delete the slices that has zeros from the original 3D array A
A(:,:,idx) = []
I hope this gives you an idea how to solve your problem.
采纳的回答
Matt J
2021-4-8
Saravanan Sengottuvel's answer moved here:
Generating an example 3D array that has 10 slices
A = repmat([0 0 0 0; 1 0 0 0; 1 0 1 0],[1 1 10]);
Modifying the 4th & 8th slice to contain only zeros in rows and columns
A(:,:,4)=[0 0 0 0;0 0 0 0;0 0 0 0];
A(:,:,8)=[0 0 0 0;0 0 0 0;0 0 0 0]
Find the slice index in 3rd dimension that has only zeros in rows and columns
idx = all(A == 0, [1 2]);
Now, delete the slices that has zeros from the original 3D array A
A(:,:,idx) = []
I hope this gives you an idea how to solve your problem.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!