How to keep column of removed row in matrix
1 次查看(过去 30 天)
显示 更早的评论
I have matrix A and want to remove the rows of A that have nonzero element. Also want to keep the index of removed columns and rows.
So, when row 3 is removed nonzero element in column 4 and 6 is removed too. I want to keep removed index [3,4,6].
Next we go to remove row 5, so nonzero element in column 6 is removed. want to keep [5,6].
result should be
removed_index={[3,4,6],[5,6]}
A=[0 0 0 0 0 0 0; 0 0 0 0 0 0 0; 0 0 0 1 0 1 0; 0 0 0 0 0 0 0; 0 0 0 0 0 1 0; 0 0 0 0 0 0 0; 0 0 0 0 0 0 0];
[rId, cId] = find(A);
removed_index=[]
for i=1:length(rId)
A(rId(i),:) = 0;
A(:,rId(i)) = 0;
end
0 个评论
采纳的回答
darova
2020-2-22
编辑:darova
2020-2-22
It was harder that i thought
clc,clear
% create some data
A = zeros(10);
ix = randi(100,1,10); % random indices to put 'ones'
A(ix) = 1;
imshow(A,'initialmagnification','fit')
axis on
[rId, cId] = find(A);
n = length( unique(rId) ); % number of cells needed
removed_index = cell(n,1);
[srId,ix] = sort(rId);
scId = cId(ix);
ir = 1;
i1 = 1;
for i = 1:length(srId)-1
if srId(i) ~= srId(i+1)
removed_index{ir} = [srId(i1) scId(i1:i)'];
ir = ir + 1;
i1 = i + 1;
end
end
removed_index{ir} = [srId(i1) scId(i1:end)'];
removed_index{:}
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!