Removing zeroes from 3d matrrix

조회 수: 4 (최근 30일)
Chris Dan
Chris Dan 2020년 6월 27일
편집: Matt J 2020년 6월 27일
Hi,
I want to remove the zeros from a 3D matrix and bring the non zeroes values upwards. I am attaching the picture which will explain more clearly. I dont want to reduce slices..
The zeroes should be removed and non zeroes entries should face each other.
I looked at these links
But it is not working for me...
does anyone has an idea..?

답변 (2개)

Matt J
Matt J 2020년 6월 27일
편집: Matt J 2020년 6월 27일
reshape(nonzeros(Fx),11,2,3);

Image Analyst
Image Analyst 2020년 6월 27일
You cannot do that for a 3-D matrix. You must have the same number of elements even if you shift things around, unless you have a whole plane in the X, Y, or Z dimension that is all zeros. In that case you can remove a whole plane. Otherwise it's not clear which dimension and direction the column goes and what direction to shift it in.
Your screenshot shows a 2-D matrix, and for that, you can shift one column up if that's what you want to do. Untested code:
[rows, columns, slices] = size(m); % m is your array.
for col = 1 : columns
thisColumn = m(:, col);
% Remove zeros
thisColumn(thisColumn == 0) = [];
% Erase whole column at first.
m(:, col) = 0;
% Put back in the nonzero elements at the top.
m(1 : length(thisColumn), col) = thisColumn;
end

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by