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

2 次查看(过去 30 天)
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 个评论
Guillaume
Guillaume 2016-6-20
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 -?

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2016-6-20
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 个评论
Guillaume
Guillaume 2016-6-20
" 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 2016-6-20
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.

请先登录,再进行评论。

更多回答(2 个)

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

请先登录,再进行评论。


Shameer Parmar
Shameer Parmar 2016-6-20
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 个评论
Guillaume
Guillaume 2016-6-20
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 2016-6-20
编辑:Guillaume 2016-6-20
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by