将矩阵中的NaN替换为其他值

时间:2016-05-26 07:21:54

标签: matlab

我有一个名为rf_rate的矩阵。有些行是NaN。我希望用rf_rate matrix的某一行替换这些NaN。到目前为止,我有下面的代码没有给我正确的答案。

miss_rf = isnan(rf_rate);    % getting a logical matrix of where NaN's are

% this us_rf is a row from the rf_rate matrix (us_pos is a single number)
us_rf = repmat(rf_rate(us_pos, :), length(rf_rate(:, 1)), 1);

% this is where its going wrong
rf_rate(miss_rf==1, :)          = us_rf(miss_rf(miss_rf==1), :);

我的矩阵大小为56x11,但在最后一行之后它变为611x11,为什么?

在我的矩阵rf_rate中有两行NaN我想要替换这两行。

2 个答案:

答案 0 :(得分:1)

如果您只有NaN个值的整行,并且'清除'其他地方的行:

% Number of rows to replace :
Nrows=sum(all(isnan(rf_rate),2));

% Replace the rows if Nrows>0:

if Nrows>0  

    rf_rate(all(isnan(rf_rate),2),:)=repmat(rf_rate(us_pos,:),Nrows,1);

end

如果您有一行参考而且您想要,每次NaN出现在任何其他行中时,要将其替换为您的参考行中的相应值:

% Create a column vector of length `numel(rf_rate)` by repeating the first row :
Corrector=reshape(repmat(rf_rate(us_pos,:),size(rf_rate,1),1),[],1);

% Use linear indexing to fetch the NaN locations and replace them :
rf_rate(isnan(rf_rate(:)))=Corrector(isnan(rf_rate(:)));

示例1:

rf_rate=magic(10);
rf_rate([2 3 7],:)=NaN;
us_pos=1;

rf_rate =

    92    99     1     8    15    67    74    51    58    40
   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN
   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN
    85    87    19    21     3    60    62    69    71    28
    86    93    25     2     9    61    68    75    52    34
    17    24    76    83    90    42    49    26    33    65
   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN
    79     6    13    95    97    29    31    38    45    72
    10    12    94    96    78    35    37    44    46    53
    11    18   100    77    84    36    43    50    27    59

% Apply code 1 :

% Number of rows to replace :
Nrows=sum(all(isnan(rf_rate),2));

% Replace the rows if Nrows>0:

if Nrows>0  

    rf_rate(all(isnan(rf_rate),2),:)=repmat(rf_rate(us_pos,:),Nrows,1);

end

<强>输出

rf_rate =

    92    99     1     8    15    67    74    51    58    40
    92    99     1     8    15    67    74    51    58    40
    92    99     1     8    15    67    74    51    58    40
    85    87    19    21     3    60    62    69    71    28
    86    93    25     2     9    61    68    75    52    34
    17    24    76    83    90    42    49    26    33    65
    92    99     1     8    15    67    74    51    58    40
    79     6    13    95    97    29    31    38    45    72
    10    12    94    96    78    35    37    44    46    53
    11    18   100    77    84    36    43    50    27    59

示例2:

rf_rate=magic(10);
us_pos=1;
NanPos=randi(numel(rf_rate),10,1);
NanPos(mod(NanPos,10)==1)=[];
rf_rate(NanPos)=NaN;

rf_rate =

 92    99     1     8    15    67    74    51    58    40
 98    80     7    14    16    73    55    57    64    41
  4   NaN    88    20    22    54    56   NaN    70   NaN
 85    87    19    21     3    60    62    69    71    28
 86    93    25     2     9    61    68    75    52    34
 17    24    76    83    90    42    49    26    33    65
 23     5    82    89    91    48    30    32    39    66
 79     6    13    95    97    29    31    38    45    72
NaN    12    94   NaN   NaN    35    37    44   NaN    53
 11    18   NaN    77    84    36   NaN    50    27    59

% Apply code 2 :

% Create a column vector of length `numel(rf_rate)` by repeating the first row :
Corrector=reshape(repmat(rf_rate(us_pos,:),size(rf_rate,1),1),[],1);

% Use linear indexing to fetch the NaN locations and replace them :
rf_rate(isnan(rf_rate(:)))=Corrector(isnan(rf_rate(:)));

输出

rf_rate =

    92    99     1     8    15    67    74    51    58    40
    98    80     7    14    16    73    55    57    64    41
     4    99    88    20    22    54    56    51    70    40
    85    87    19    21     3    60    62    69    71    28
    86    93    25     2     9    61    68    75    52    34
    17    24    76    83    90    42    49    26    33    65
    23     5    82    89    91    48    30    32    39    66
    79     6    13    95    97    29    31    38    45    72
    92    12    94     8    15    35    37    44    58    53
    11    18     1    77    84    36    74    50    27    59

请注意,该代码的第二个版本适用于机器人案例。

答案 1 :(得分:0)

我还没弄清楚你的代码到底发生了什么,但是根据你的描述,我认为你想要的是

rf_rate(isnan(rf_rate)) = rf_rate(us_pos,:);

请注意,与您编写的内容不同,us_rf不是一行,而是重复行的矩阵。你的代码正在接受并多次插入。

另请注意,这不是特别强大,如果您的个人NaN没有占用整行,则会失败。

相关问题