Matrix Filtering
Afficher commentaires plus anciens
I have an nxn matrix of 0's and 1's, eg:
1 0 0 0 0 0
1 1 0 0 0 0
1 1 0 0 0 0
1 0 0 1 0 0
0 0 0 1 1 0
0 0 0 0 1 1
I wish to zero any values to the right of a 1 in a preceding column, and additionally if the 1 on the diagonal is removed, then all the subsequent values in that column become 0, and do not affect any other colums, such that I would end up with the following:
1 0 0 0 0 0
1 0 0 0 0 0
1 0 0 0 0 0
1 0 0 0 0 0
0 0 0 0 1 0
0 0 0 0 1 0
Preferably not using a loop!
Note the 1 at (4,1) causes the 1s in the 4 column to be 0ed, and the 1 at (5,5) is not removed, because the 1 at (5,4) has been 0ed.
Any ideas?
Thanks...
1 commentaire
Matt Fig
le 5 Juin 2011
I think you mean the 1 at (4,4), not (4,1). Also, why would this one at (4,4) be zeroed when it doesn't follow a 1??
Réponses (2)
Matt Fig
le 5 Juin 2011
Say your array is called B
B([false(size(B,1),1),diff(B,[],2)==0] & B) = 0;
Oops, I didn't see the diagonal condition. Hmm...
%
%
%
EDIT
idx = [false(size(B,1),1),diff(B,[],2)==0] & B & eye(size(B));
B(logical(cumsum(idx))) = 0;
B([false(size(B,1),1),diff(B,[],2)==0] & B) = 0;
%
%
%
EDIT 2 After more clarification...
In that case, I think a loop may be your best bet... Perhaps someone else will come up with another way.
for ii = 1:size(A,1)
idx = find(A(ii,1:n-1),1);
if ~isempty(idx)
if idx<ii && A(ii,ii)
A(ii:m,ii) = 0;
end
A(ii,idx+1:n) = 0;
end
end
Andrei Bobrov
le 7 Juin 2011
for our case
[m,n]=size(A);
II = mat2cell(A,m,ones(1,n));
I3 = nchoosek(1:n,2);
jj = I3(arrayfun(@(x)all(xor(II{I3(x,1)},II{I3(x,2)})),1:size(I3,1)),:);
I4 = zeros(size(A));
I4(:,jj) = A(:,jj);
Catégories
En savoir plus sur Time Series Objects dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!