MATLAB Answers

Getting an unknown error on calculating Standard Deviation.

조회 수: 4(최근 30일)
I was trying to calculation using inbuilt Standard Deviation function but getting an error.
I ran the following code
load myfile.mat
std(v)
Error:
Error using var (line 74)
Invalid data type. First input argument must be single or double.
Error in std (line 59)
y = sqrt(var(varargin{:}));

  댓글 수: 1

Atul Upadhyay
Atul Upadhyay 19 Jun 2020
I can see that the vector you are using has the int8 datatype in it and it contains values from 0 to 100. So, while calculating standard deviation there are chances of overflow.
Typecast the vector v to ‘double’ to get the desired output.
load myfile.mat;
v=double(v);
sd=std(v);
Refer to the following documentation to know more about datatypes in MATLAB.
https://in.mathworks.com/help/matlab/matlab_prog/integers.html

Sign in to comment.

채택된 답변

Atul Upadhyay
Atul Upadhyay 19 Jun 2020
I can see that the vector you are using has the int8 datatype in it and it contains values from 0 to 100. So, while calculating standard deviation there are chances of overflow.
Typecast the vector v to ‘double’ to get the desired output.
load myfile.mat;
v=double(v);
sd=std(v);
Refer to the following documentation to know more about datatypes in MATLAB.
https://in.mathworks.com/help/matlab/matlab_prog/integers.html

  댓글 수: 0

Sign in to comment.

추가 답변(1개)

Bjorn Gustavsson
Bjorn Gustavsson 19 Jun 2020
Your variable v is a uint8. Try:
std(double(v))
HTH

  댓글 수: 0

Sign in to comment.

태그

제품


릴리스

R2020a

Translated by