mario
mario

Reputation: 75

replace first (x number) non-nan values with nan

suppose I have the following matrix: a =

 2   NaN   NaN
 4   NaN     3
 3     7     9
 5    12     5
 8    10     8
12     5    10

I need to replace the first x number non-Nan values in each column with Nan.

If number of values to be replaced is x = 3, then the new matrix should be:

b =

NaN   NaN   NaN

NaN   NaN   NaN

NaN   NaN   NaN

5     NaN   NaN

8     NaN   8

12    5     10

Any ideas how to do this?

Thanks in advance.

Upvotes: 1

Views: 648

Answers (4)

Amro
Amro

Reputation: 124573

Here is another vectorized code:

%# given the data
a = [ 2 NaN NaN; 4 NaN 3; 3 7 9; 5 12 5; 8 10 8; 12 5 10 ]
x = 3

%# replace with NaNs
sz = size(a);
d = [ones(1,sz(2)) ; diff(~isnan(a))];
rIdx = arrayfun(@(k) find(d(:,k),1,'last'), 1:sz(2));
ind = bsxfun(@plus, sub2ind(sz, rIdx, 1:sz(2)), (0:x-1)');
a(ind(:)) = NaN;

First we check non-nan elements, then we diff the result across the rows. We find the location of the last 1 in each column, convert to linear indices and add the offset x to each. Finally we use the computed indices to replace with NaNs.

Upvotes: 2

nrz
nrz

Reputation: 10580

This is a vectorized solution. First get the top part of a (the part that will be replaced with new NaNs) into aTopMatrix. Then get get lower part of a into aLowMatrix. Then replace values of aLowMatrix with NaNs according to the pre-existing NaN values in aTopMatrix by using logical addressing. Finally, create a NaN array sized x x size(a,2) and concatenate it vertically with aLowMatrix to get the desired result matrix in b.

% Define the example data:

a = [ 2 NaN NaN; 4 NaN 3; 3 7 9; 5 12 5; 8 10 8; 12 5 10 ]
x = 3

% Here's the code:

aTopMatrix = a(1:x, 1:end);
aLowMatrix = a(x+1:end, 1:end);
aLowMatrix(isnan(aTopMatrix)) = NaN;
b = [ ones(x, size(a,2))*NaN; aLowMatrix ];

Upvotes: 2

Surender Thakran
Surender Thakran

Reputation: 4054

class TestNan
{
    public static void main(String[] args)
    {
        double[][] mat = new double[6][3];
        //initialize the matrix here
        for(int i = 0; i<3; i++)
        {
            int x = 3; // no. of numbers to be replaced
            for(int j = 0; j<6; j++)
            {
                if(x == 0)
                    break;
                Double d = Double.valueOf(mat[i][j]);
                if(!d.isNaN())
                {
                    d = Double.NaN;
                    x--;
                }
            }
        }
        //Print your matrix here
    }   
}

Try this, let me know if you encounter any problem!!

Upvotes: -1

lxop
lxop

Reputation: 8625

Loop through the columns, then loop through the members of each column, replacing the first 3 non-NaN numbers with Nan:

for c = 1:size (a,2)
  col = a (:,c);
  replaced = 0;
  for r = 1:size (col)
    if (~isnan (col (r)))
      a (r,c) = Nan;
      replaced = replaced + 1
      if (replaced == 3)
        break;
      end
    end
  end
end

I think that should do it

Upvotes: 2

Related Questions