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

5 visualizaciones (últimos 30 días)
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]

Respuesta aceptada

Image Analyst
Image Analyst el 29 de Nov. de 2020
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

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by