How to keep column of removed row in matrix

3 次查看(过去 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

采纳的回答

darova
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{:}
  2 个评论
NA
NA 2020-2-22
I want to get this result,
removed_index={[3,4,6],[5,6]}.
darova
darova 2020-2-22
I created the code. Look at my previous post

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by