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

3 ビュー (過去 30 日間)
ellora
ellora 2016 年 6 月 20 日
コメント済み: ellora 2016 年 6 月 20 日
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 -?
ellora
ellora 2016 年 6 月 20 日
yeah sure, it is (u(i+1) - u(i-1))/2

サインインしてコメントする。

採用された回答

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 Center および File ExchangeWhos についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by