How to keep column of removed row in matrix

3 views (last 30 days)
NA
NA on 22 Feb 2020
Edited: NA on 22 Feb 2020
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

Accepted Answer

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

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!