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

3 views (last 30 days)
in the matrix i have an strange number 999, I need to perform an averaging between two numbers to get a new value of u(i) u(i)=(u(i+1)-u(i-1))/2 but if u(i),or u(i+1) or u(i-1) comes 999 then it should skip this row and jump to next value in the table u(i). so that when i will get the new value of u(i), the value 999 remain undisturbed in it position and other value should not get affected by this value.
  2 Comments
Guillaume
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 -?

Sign in to comment.

Accepted Answer

Guillaume
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
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.
ellora
ellora on 20 Jun 2016
here is the file. the procedure you have used is correct, but i am unable to do it for all the columns at a time.

Sign in to comment.

More Answers (2)

KSSV
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
Guillaume on 20 Jun 2016
find is not required most of the time and is usually a waste of time. You can use the logical array directly.

Sign in to comment.


Shameer Parmar
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
Guillaume on 20 Jun 2016
I know it fails because for i = 4 for example you're averaging 86 with 999 which is not "other value should not get affected by this value"
ellora
ellora on 20 Jun 2016
Edited: Guillaume on 20 Jun 2016
Guillaume i need a help, your solution is correct for single column, but how it can be done for 891x3 matrix?can u please explain.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!