how to replace NaN values with zero?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hello,
I have a matrix of 361*181 dimention, which contain NaN and some values.
I want to replace those NaN values with 0, I have tried A(isnan(A))=0; command but is showing error.
the error read as " Subscript indices must either be real positive integers or logicals".
can anyone help me out this?
2 件のコメント
Walter Roberson
2018 年 12 月 31 日
What shows up for
which -all isnan
Puspa patra
2018 年 12 月 31 日
Thanks
採用された回答
madhan ravi
2018 年 12 月 31 日
編集済み: madhan ravi
2018 年 12 月 31 日
Might happen that your isnan is somehow a variable maybe try the following:
clear all
A(isnan(A))=0;
%%%% an example
A=rand(3);
A([2 3],2)=NaN;
A(isnan(A))=0;
Gives:
A =
0.7339 0.8422 0.1934
0.0039 0 0.3316
0.1183 0 0.6213
12 件のコメント
Puspa patra
2018 年 12 月 31 日
Same error,
By the way there is no difference between your code and mine.
madhan ravi
2018 年 12 月 31 日
In case if you didn‘t notice mine has clear all at the very beginning which is used to clear the variables from the workspace in case if you have any variable named isnan it will be cleared , this is one way of clearing the error.
madhan ravi
2018 年 12 月 31 日
Type in command window the following and paste the output here
whos isnan
whos A
which isnan -all
Puspa patra
2018 年 12 月 31 日
Name Size Bytes Class Attributes
isnan 1x0 0 double
Name Size Bytes Class Attributes
A 361x181 522728 double
isnan is a variable.
built-in (/usr/local/MATLAB/R2015b/toolbox/matlab/elmat/@double/isnan) % Shadowed double method
built-in (/usr/local/MATLAB/R2015b/toolbox/matlab/elmat/@uint8/isnan) % Shadowed uint8 method
built-in (/usr/local/MATLAB/R2015b/toolbox/matlab/elmat/@uint16/isnan) % Shadowed uint16 method
built-in (/usr/local/MATLAB/R2015b/toolbox/matlab/elmat/@uint32/isnan) % Shadowed uint32 method
built-in (/usr/local/MATLAB/R2015b/toolbox/matlab/elmat/@uint64/isnan) % Shadowed uint64 method
built-in (/usr/local/MATLAB/R2015b/toolbox/matlab/elmat/@int8/isnan) % Shadowed int8 method
built-in (/usr/local/MATLAB/R2015b/toolbox/matlab/elmat/@int16/isnan) % Shadowed int16 method
built-in (/usr/local/MATLAB/R2015b/toolbox/matlab/elmat/@int32/isnan) % Shadowed int32 method
built-in (/usr/local/MATLAB/R2015b/toolbox/matlab/elmat/@int64/isnan) % Shadowed int64 method
built-in (/usr/local/MATLAB/R2015b/toolbox/matlab/elmat/@single/isnan) % Shadowed single method
built-in (/usr/local/MATLAB/R2015b/toolbox/matlab/elmat/@char/isnan) % Shadowed char method
built-in (/usr/local/MATLAB/R2015b/toolbox/matlab/elmat/@logical/isnan) % Shadowed logical method
/usr/local/MATLAB/R2015b/toolbox/matlab/timefun/@duration/isnan.m % Shadowed duration method
/usr/local/MATLAB/R2015b/toolbox/matlab/timefun/@calendarDuration/isnan.m % Shadowed calendarDuration method
/usr/local/MATLAB/R2015b/toolbox/symbolic/symbolic/@sym/isnan.m % Shadowed sym method
/usr/local/MATLAB/R2015b/toolbox/ident/ident/@iddata/isnan.m % Shadowed iddata method
/usr/local/MATLAB/R2015b/toolbox/ident/nlident/@idnlmodel/isnan.m % Shadowed idnlmodel method
/usr/local/MATLAB/R2015b/toolbox/ident/nlident/@idnlgrey/isnan.m % Shadowed idnlgrey method
/usr/local/MATLAB/R2015b/toolbox/distcomp/parallel/@codistributed/isnan.m % Shadowed codistributed method
/usr/local/MATLAB/R2015b/toolbox/distcomp/gpu/@gpuArray/isnan.m % Shadowed gpuArray method
Puspa patra
2018 年 12 月 31 日
this is the output
Walter Roberson
2018 年 12 月 31 日
isnan is a variable.
That is the important point. You assigned something to isnan at some point, which damages the ability to use isnan() as a function.
madhan ravi
2018 年 12 月 31 日
See as I told you , it's a variable
The solution is
clear isnan
% or
clear all
% or
clear global
Puspa patra
2018 年 12 月 31 日
Thanks a lot.... it worked
madhan ravi
2018 年 12 月 31 日
Anytime :)
Fabricio Arturo Balboa Cavazos
2021 年 4 月 1 日
Thanks madhan ravi!
Ashishkumar Gupta
2023 年 1 月 5 日
Tried ur code for my problem (https://de.mathworks.com/matlabcentral/answers/1889142-how-to-remove-nans-from-struct) but did not work out!!
I want to replace all the NaN with 0 so that size is same for future use.
Ashishkumar Gupta The png attachments in your linked post are not informative. It might be you are not accessing the structure data correclty to get the value you wish to set to zero.
For example, you have a struct named WS_struct with several fields. To access the values (and assuming they are numeric), you would type: WS_struct.HP_EDU_00571_eTA
Just be sure that the fields give numeric data and you correctly access the data. The data fields in the other pic are double, so the method outlined about should work and you can explore with this code how to manipulate the struct fields and set to new named varibles and access entries:
%% example code to set all nan entries to zero after accessing from a struc file
clear all; clc;
M = 5*rand(5,5); % make a 5x5 matrix with random nums on [0,5]
n = randi([1 3],1); % rand num from 1 to 3 to use as the num of nans
randRows = randi([1 5],n,1); % get n rand rows from the 5 cols
randCols = randi([1 5],1,n); % get n rand cols from the 5 cols
fprintf('\nA random 5x5 matrix with 1 to 9 random nan entries:\n');
M(randRows,randCols) = nan
% Now create a structure with field randM and set to M
S.randnum = n;
S. randRows = randRows
S. randCols = randCols;
S.randM = M;
save('randomMatrix.mat','S'); % save structure S
% clear all memory and access data directly from struct S
clear all; % mimicking someone handing you the structure for the first time
load randomMatrix.mat
% set matrix newM to S.randM
newM = S.randM;
fprintf('\nSetting all nan entries to zero:\n');
newM(isnan(newM)) = 0 % find nan entries and set to zero
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Structures についてさらに検索
製品
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
