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

6 ビュー (過去 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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by