How to keep column of removed row in matrix

3 Ansichten (letzte 30 Tage)
NA
NA am 22 Feb. 2020
Bearbeitet: NA am 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

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by