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

3 visualizzazioni (ultimi 30 giorni)
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]

Risposta accettata

Image Analyst
Image Analyst il 29 Nov 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

Più risposte (0)

Categorie

Scopri di più su Cell Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by