milli
milli

Reputation: 75

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

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

Upvotes: 7

Views: 1882

Answers (1)

angainor
angainor

Reputation: 11810

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.

Upvotes: 6

Related Questions