Thread Subject: setting the matrix elements to zero when...

Subject: setting the matrix elements to zero when...

From: SM

Date: 8 Feb, 2010 15:50:19

Message: 1 of 9

Hi,
I have my data sorted in a n x m matrix.
I want to check the data in each row, and see if there are more than for instance 5 non-zero elements in-row, set those values to zero,and check to the last element in each row and do the same thing.
e.g.:
input=[1 2 1 1 1 0 0 0 0 1 1 1 2 2 3 1 0 0 0
         1 0 1 1 0 0 0 0 0 1 0 1 2 0 3 1 0 0 0
         ...
         1 0 1 1 1 0 1 0 1 1 3 1 2 2 0 0 0 0 0]

output = [ 1 2 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
                1 0 1 1 0 0 0 0 0 1 0 1 2 0 3 1 0 0 0
                ...
                1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0]
my own code is:

for i=1:size(input,1)
for j=1:5:length(input)-5
if input(i,j) && input(i,j+1) && input(i,j+2) && input(i,j+3) && input(i,j+4)~=0
input(i,j)=0
input(i,j+1)=0
input(i,j+2)=0
input(i,j+3)=0
input(i,j+4)=0
end
end
end

This code works for the 5 non-zero elements. How should I modify this for the cases where I have 5 or more than 5 non-zero elements in row? Anybody there who could help me with this?
Thanx in advance

Subject: setting the matrix elements to zero when...

From: Oleg Komarov

Date: 8 Feb, 2010 17:21:04

Message: 2 of 9

"SM "
> Hi,
> I have my data sorted in a n x m matrix.
> I want to check the data in each row, and see if there are more than for instance 5 non-zero elements in-row, set those values to zero,and check to the last element in each row and do the same thing.
> e.g.:
> input=[1 2 1 1 1 0 0 0 0 1 1 1 2 2 3 1 0 0 0
> 1 0 1 1 0 0 0 0 0 1 0 1 2 0 3 1 0 0 0
> ...
> 1 0 1 1 1 0 1 0 1 1 3 1 2 2 0 0 0 0 0]
>
> output = [ 1 2 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> 1 0 1 1 0 0 0 0 0 1 0 1 2 0 3 1 0 0 0
> ...
> 1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0]
> my own code is:
>
> for i=1:size(input,1)
> for j=1:5:length(input)-5
> if input(i,j) && input(i,j+1) && input(i,j+2) && input(i,j+3) && input(i,j+4)~=0
> input(i,j)=0
> input(i,j+1)=0
> input(i,j+2)=0
> input(i,j+3)=0
> input(i,j+4)=0
> end
> end
> end
>
> This code works for the 5 non-zero elements. How should I modify this for the cases where I have 5 or more than 5 non-zero elements in row? Anybody there who could help me with this?
> Thanx in advance


Tolerance = 5;

In = [1 2 1 1 1 0 0 0 0 1 1 1 2 2 3 1 0 0 0
      1 0 1 1 0 0 0 0 0 1 0 1 2 0 3 1 0 0 0
      1 0 1 1 1 0 1 0 1 1 3 1 2 2 0 0 0 0 0];

Out = In.';
% Replace all non zero values with 1
In(In ~= 0) = 1;
% Create the bread
bread = NaN(size(In,1),1);
% Make a sandwich with In as the meat
sandwich = cat(2,bread,In,bread);
% Find chunks
IDX = diff(diff(sandwich,1,2) == 0,1,2).';
% Initial positions, final positions and lenght of blocks
inPos = find(IDX == 1);
fiPos = find(IDX == -1);
Lengths = fiPos - inPos + 1;

% Out
for s = find(Lengths >= Tolerance).'
    Out(inPos(s):fiPos(s)) = 0;
end
% Reshape it back
Out = Out.';

Note that this approach works row-wise.

Oleg

Subject: setting the matrix elements to zero when...

From: SM

Date: 9 Feb, 2010 15:05:20

Message: 3 of 9

Thanx so much!

Subject: setting the matrix elements to zero when...

From: Oleg Komarov

Date: 9 Feb, 2010 18:05:22

Message: 4 of 9

"SM "
> Thanx so much!
You're welcome.
Oleg

Subject: setting the matrix elements to zero when...

From: Matt Fig

Date: 9 Feb, 2010 19:43:05

Message: 5 of 9

Just as a note, you cold have made your loop method more efficient by skipping columns which were already overwritten, for any length. This might even be faster than the partial vectorization Oleg showed, especially when the matrix has few zeros. Here is one way to do it:


[r,c] = size(A);
cnt = 1;

for ii = 1:r
    jj = 1;
    while jj <= (c-1) % Use a WHILE loop here to control search increment.
        if A(ii,jj)
            while (jj+cnt)<=c && A(ii,jj+cnt)
                cnt = cnt + 1;
            end
            if cnt >= Tolerance
                A(ii,jj:jj+cnt-1) = 0;
                cnt = cnt + 1;
            end
        end
        jj = jj + cnt; % Skips columns already set to zero.
        cnt = 1;
    end
end

Subject: setting the matrix elements to zero when...

From: SM

Date: 9 Feb, 2010 22:52:02

Message: 6 of 9

Hey Guys,
I've got stuck again! What if, there is an NaN element in the matrix?

Subject: setting the matrix elements to zero when...

From: Oleg Komarov

Date: 10 Feb, 2010 08:19:02

Message: 7 of 9

"SM " <khanmoradi@gmail.com> wrote in message <hksoui$3va$1@fred.mathworks.com>...
> Hey Guys,
> I've got stuck again! What if, there is an NaN element in the matrix?
Treating NaNs in a run lenght econding algorithm requires you to treat first all non NaN elements and then separataly those which are NaN. (because diff(NaN, NaN) = NaN).

Meaning that B = isnan(A); then treat B.

Oleg

Subject: setting the matrix elements to zero when...

From: SM

Date: 10 Feb, 2010 19:06:05

Message: 8 of 9

Thanx! and sorry for all these disturbing questions. May sound stupid, but I am not an expert in MATLAB programming.
I've got one more question if you don't mind!
As I explained I was trying to set the data to zero if they are 5 or more non-zero values in row! Now, if I want to add one more constraint , in a way that if there is for instance less than 2 zeros in between, just ignores those 2 zero, and continue with counting the rest to check whether they are more than 5 or not adn if so, set them to zero, would it be possible to do so?
I was trying to do it with setting a counter and...but my code simply sucks!

for instance,
Tolerance=5;
Vol=2;
In=[1 2 0 1 0 5 1 0 0 0 0 7;...
     1 0 0 1 1 2 3 4 0 1 0 1;...
     1 0 0 0 0 0 6 0 0 0 0 5];

Out=[0 0 0 0 0 0 0 0 0 0 7;...
         1 0 0 0 0 0 0 0 0 0 0;...
         1 0 0 0 0 0 6 0 0 0 5];

Thanx again for all your help
Cheers

Subject: setting the matrix elements to zero when...

From: SM

Date: 10 Feb, 2010 19:44:20

Message: 9 of 9

I am really getting crazy...!!!! My brain is not functioning anymore and the deadline is approaching:(...Urgent helpppppppppp guysssssss!!!!
--------------------------------------------------------------------------
Thanx! and sorry for all these disturbing questions. May sound stupid, but I am not an expert in MATLAB programming.
I've got one more question if you don't mind!
As I explained I was trying to set the data to zero if they are 5 or more non-zero values in row! Now, if I want to add one more constraint , in a way that if there is for instance less than 2 zeros in between, just ignores those 2 zero, and continue with counting the rest to check whether they are more than 5 or not adn if so, set them to zero, would it be possible to do so?
I was trying to do it with setting a counter and...but my code simply sucks!

for instance,
Tolerance=5;
Vol=2;
In=[1 2 0 1 0 5 1 0 0 0 0 7;...
     1 0 0 1 1 2 3 4 0 1 0 1;...
     1 0 0 0 0 0 6 0 0 0 0 5];

Out=[0 0 0 0 0 0 0 0 0 0 7;...
         1 0 0 0 0 0 0 0 0 0 0;...
         1 0 0 0 0 0 6 0 0 0 5];

Thanx again for all your help
Cheers

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com