# how to ignore a value and jumps to next row for calculation?

2 views (last 30 days)

Show older comments

##### 2 Comments

Guillaume
on 20 Jun 2016

ellora, could you please give a short example of input and expected output.

Are you sure you want (u(i+1) - u(i-1))/2 and not (u(i+1) + u(i-1))/2, note the + instead of -?

### Accepted Answer

Guillaume
on 20 Jun 2016

Probably the simplest thing, is to store the location of the 999, remove them, perform your averaging (which is then very straightforward as you don't have special cases anymore) and finally put back the 999:

v = [1 2 3 999 5 6 7 10 999 20 40 80 160] %example data

isspecial = v == 999; %logical array indicating the position of 999

filteredv = v(~isspecial); %remove 999 from vector

%do your averaging any way you want. Not sure what you want to do at the edges

filteredv = conv(filteredv, [0.5 0 0.5], 'same'); %does u(i)=(u(i+1)-u(i-1))/2, use zero-padding at the edges

%now create a vector with filtered values and 999 in their original location

finalv = zeros(size(v));

finalv(~isspecial) = filteredv;

finalv(isspecial) = v(isspecial)

##### 7 Comments

Guillaume
on 20 Jun 2016

" my columns do not have equal number of 999 values". I assumed as much, the code above does not assume that there is the same number of 999 in each column.

Yes, please attach your matrix (as a mat file please), and show what output you expect for the first few values.

### More Answers (2)

KSSV
on 20 Jun 2016

You can get the indices of 999 using find..

idx = find(mymatrix==999)

Now you can do averaging by excluding idx..or make idx to zero and get avarage...

##### 1 Comment

Guillaume
on 20 Jun 2016

Shameer Parmar
on 20 Jun 2016

Let us consider, you have values as follows as example:

u = [112, 54, 86, 547, 999, 458, 657, 999, 56, 422, 100];

for i = 2:(length(u)-1)

if (u(i) ~= 999)

u(i) = (u(i+1)-u(i-1))/2;

else

u(i) = 999;

end

end

##### 4 Comments

Guillaume
on 20 Jun 2016

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!