How do I delete slices in 3D-Array?

조회 수: 4 (최근 30일)
Kay Schmidt
Kay Schmidt 2021년 4월 8일
편집: Stephen23 2021년 4월 8일
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
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.
Kay Schmidt
Kay Schmidt 2021년 4월 8일
편집: Stephen23 2021년 4월 8일
Thanks, works perfectly!

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

채택된 답변

Matt J
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]
A =
A(:,:,1) = 0 0 0 0 1 0 0 0 1 0 1 0 A(:,:,2) = 0 0 0 0 1 0 0 0 1 0 1 0 A(:,:,3) = 0 0 0 0 1 0 0 0 1 0 1 0 A(:,:,4) = 0 0 0 0 0 0 0 0 0 0 0 0 A(:,:,5) = 0 0 0 0 1 0 0 0 1 0 1 0 A(:,:,6) = 0 0 0 0 1 0 0 0 1 0 1 0 A(:,:,7) = 0 0 0 0 1 0 0 0 1 0 1 0 A(:,:,8) = 0 0 0 0 0 0 0 0 0 0 0 0 A(:,:,9) = 0 0 0 0 1 0 0 0 1 0 1 0 A(:,:,10) = 0 0 0 0 1 0 0 0 1 0 1 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) = []
A =
A(:,:,1) = 0 0 0 0 1 0 0 0 1 0 1 0 A(:,:,2) = 0 0 0 0 1 0 0 0 1 0 1 0 A(:,:,3) = 0 0 0 0 1 0 0 0 1 0 1 0 A(:,:,4) = 0 0 0 0 1 0 0 0 1 0 1 0 A(:,:,5) = 0 0 0 0 1 0 0 0 1 0 1 0 A(:,:,6) = 0 0 0 0 1 0 0 0 1 0 1 0 A(:,:,7) = 0 0 0 0 1 0 0 0 1 0 1 0 A(:,:,8) = 0 0 0 0 1 0 0 0 1 0 1 0
I hope this gives you an idea how to solve your problem.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by