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]
0 个评论
采纳的回答
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 Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!