Replace NaN values with a formula

6 ビュー (過去 30 日間)
Caleb
Caleb 2014 年 1 月 15 日
コメント済み: Caleb 2014 年 1 月 15 日
%I am looking to replace NaN values in a given matrix using a formula.
%Suppose that row 1 of matrix A are totals (See column 3).
%Row 2-4 are subsectors that when summed equal the total.
A = [1100 1200 1125 1635 1850; 45 22 NaN 35 51; NaN NaN 510 600 782; 732 522 534 1000 NaN]
%Another matrix C corresponds to a particular row's average percentage of the total.
C =
0.0281 0.4143 0.5633
%I want to replace each NaN value by multiplying the Total by the coresponding average for that row.
%ie. The NaN value that occurs at A(3,1) would be replaced by .4143*1100=414.3
%any help would be appreciated.

採用された回答

Bruno Pop-Stefanov
Bruno Pop-Stefanov 2014 年 1 月 15 日
Hi Caleb,
You can use the function isnan to check if an element of A is not a number. In your case, I would go through every element of A, check if it is NaN and replace it with your formula using C. This can be done with a simple for loop as follows:
for row=2:size(A,1)
for col=1:size(A,2)
if isnan(A(row,col))
A(row,col) = A(1,col) * C(row-1);
end
end
end
I hope that helps!
-Bruno
  1 件のコメント
Caleb
Caleb 2014 年 1 月 15 日
Bruno,
Thank you so much for your help. It worked perfectly!
Caleb

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLanguage Fundamentals についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by