Data averaging on a specific cell of a matrix

8 ビュー (過去 30 日間)
Junayed Chowdhury
Junayed Chowdhury 2018 年 3 月 20 日
コメント済み: Junayed Chowdhury 2018 年 3 月 20 日
Hello, Say I have a matrix (A1) of single coloumn and 1000 rows ( 1000 X 1) in size. I have 0 values at random cells in it. Say row 55 has a 0 value, where row 54 has a values of 5.5 and row 56 has a value of 4.5. How to write a for loop that will find the 0 values and fill it with the averaging of its previous and after cell/row values?
thanks in advance

採用された回答

Akira Agata
Akira Agata 2018 年 3 月 20 日
You don't have to use for-loop for this. Assuming A1(1) and A1(1000) are not zero, and there is NO consecutive 0s, the following code can fill the 0 values with the averaging of its neighbourings.
idx = find(A1 == 0);
A1(idx) = (A1(idx+1)+A1(idx-1))/2;
  1 件のコメント
Junayed Chowdhury
Junayed Chowdhury 2018 年 3 月 20 日
Thanks for your answer :)

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

その他の回答 (1 件)

Junayed Chowdhury
Junayed Chowdhury 2018 年 3 月 20 日
I used this one and it worked :)
for i=1:size(A1) if A1(i,1)==0 A1(i,1)= ((A1(i+1,1)+A1(i-1,1))/2); end end

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by