I wanted to calculate the standard derivation of the elements of a matrix. So i first transformed my matrix with the command reshape
to a vector, and then used std
.
However, I got a error message:
Error using var (line 59)
First argument must be single or double.
Error in std (line 32)
y = sqrt(var(varargin{:}));
Error in reducenoise2>standabw (line 112)
s = std(B);
Error in reducenoise2 (line 36)
D = standabw(n,m,r,fu,D);
So I printed my vector B
, just before passing it to std
. I assigned it to a variable x
in REPL tried calling std(x)
manually.
Interestingly enough, this works just fine.
So how can the function std
– called with the same arguments – result in an error when used within my code, but work fine in REPL?
Here is the Matlab function:
function [D] = standabw(n,m,r,fu,D)
for i = 1+r:n-r
for j = 1+r:m-r
C = D(i-r:i+r,j-r:j+r);
B = reshape(C,(2*r+1)^2,1)
s = std(B);
if s > fu
D(i,j) = 255;
end
end
end
end
This is the vector B
, just before the error message:
B =
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
S = std( A , w , dim ) returns the standard deviation along dimension dim .
Example of Calculating Standard Error in MATLAB First, the user needs to create an array called "data" containing these observations in MATLAB. Next, the user can calculate the standard error of the mean with the command "stderror = std( data ) / sqrt( length )".
std(A,0,1) computes the standard deviation of the elements in each column of A and returns a 1 -by- n row vector. std(A,0,2) computes the standard deviation of the elements in each row of A and returns an m -by- 1 column vector.
Description. example. B = std2( A ) computes the standard deviation of all values in array A .
Most likely your B vector its of some int type. Try to call this way
std(double(B))
The above statement first casts B
to double type, and then calls std.
To check, what is the type of the variables type whos
at the command prompt.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With