how can I organize an array removing null elements and keep the same structure

조회 수: 4 (최근 30일)
Hello, everything okay?
if
A=[0 2 3 4 0 6;
0 6 8 10 0 10]
end if wont to
B= [2 3 4 6;
6 8 10 10]

채택된 답변

Image Analyst
Image Analyst 2020년 11월 29일
Try all():
A=[0 2 3 4 0 6;
0 6 8 10 0 10]
columnsToKeep = any(A ~= 0, 1)
A = A(:, columnsToKeep)
If there are not the same number of zeros in each row, then that column will not be deleted. Only columns where every element in the column is 0 will be deleted. If you have the same number or zeros in each row but they occur in ndifferent columns, then you'd need
A=[ 2 0 3 4 0 6;
0 6 8 10 0 10]
[rows, columns] = size(A)
numZeros = sum(A(1,:) == 0)
output = zeros(rows, columns - numZeros)
for row = 1 : rows
thisRow = A(row, :);
output(row, :) = thisRow(thisRow ~= 0);
end

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by