Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab's std works in REPL but not in the programm

Tags:

matlab

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
like image 605
milli Avatar asked Oct 04 '12 15:10

milli


People also ask

How do I show standard deviation in MATLAB?

S = std( A , w , dim ) returns the standard deviation along dimension dim .

How does MATLAB calculate standard error?

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 )".

How do you find the standard deviation of a column vector in MATLAB?

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.

What is std2 MATLAB?

Description. example. B = std2( A ) computes the standard deviation of all values in array A .


1 Answers

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.

like image 84
angainor Avatar answered Nov 06 '22 22:11

angainor