Matrix Filtering

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 comentario

Matt Fig
Matt Fig el 5 de Jun. de 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??

Iniciar sesión para comentar.

Respuestas (2)

Matt Fig
Matt Fig el 5 de Jun. de 2011

0 votos

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

6 comentarios

Sally
Sally el 5 de Jun. de 2011
I probably changed the question as you were answering, sorry!!
Sally
Sally el 5 de Jun. de 2011
Close, but I end up with:
B =
1 0 0 0 0 0
1 0 0 0 0 0
1 0 0 0 0 0
1 0 0 1 0 0
0 0 0 1 0 0
0 0 0 0 0 0
Matt Fig
Matt Fig el 5 de Jun. de 2011
You did change the question as I was answering...
Why would (4,4) be removed when it doesn't follow a 1??
Sally
Sally el 5 de Jun. de 2011
:) Sorry!
(4,4) follows the 1 @ (4,1) so gets removed - I'm using row,column
Sally
Sally el 5 de Jun. de 2011
Oh... I see where you're coming from... not just the preceding column having a 1... imagine the 1's to the left sweeping right across the matrix and wiping out any other 1's.
As each element on the diagonal is hit, if it becomes a 0, this then sends a wave of 0's down the matrix, removing any 1's further down the particular column...
Sally
Sally el 6 de Jun. de 2011
Thanks... I thought I was being hopeful without a loop!!

Iniciar sesión para comentar.

Andrei Bobrov
Andrei Bobrov el 7 de Jun. de 2011

0 votos

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);

Categorías

Más información sobre Performance and Memory en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 5 de Jun. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by