How to keep column of removed row in matrix

조회 수: 3 (최근 30일)
NA
NA 2020년 2월 22일
편집: NA 2020년 2월 22일
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