Search code examples
matlabangleatan2

matlab variance of angles


I have a matrix containing angles and I need to compute the mean and the variance. for the mean I procede in this way: for each angles compute sin and cos and sum all sin and all cos the mean is given by the atan2(sin, cos) and it works my question is how to compute the variance of angles knowing the mean?

thank you for the answers

I attach my matlab code:

for i=1:size(im2,1)

    for j=1:size(im2,2)
        y=y+sin(hue(i, j));
        x=x+cos(hue(i, j));
    end
end
mean=atan2(y, x);

if mean<0

    mean=mean+(2*pi);
end

Solution

  • I am not 100% sure what you are doing, but maybe this would achieve the same thing with the build in MATLAB functions mean and var.

    >> [file path] = uigetfile;
    >> someImage = imread([path file]);
    >> hsv = rgb2hsv(someImage);
    >> hue = hsv(:,:,1);
    >> m = mean(hue(:))
    
    m =
    
        0.5249
    
    >> v = var(hue(:))
    
    v =
    
        0.2074
    

    EDIT: I am assuming you have an image because of your variable name hue. But it would be the same for any matrix.

    EDIT 2: Maybe that is what you are looking for:

    >> sumsin = sum(sin(hue(:)));
    >> sumcos = sum(cos(hue(:)));
    >> meanvalue = atan2(sumsin,sumcos)
    
    meanvalue =
    
        0.5276
    
    >> sumsin = sum(sin((hue(:)-meanvalue).^2));
    >> sumcos = sum(cos((hue(:)-meanvalue).^2));
    >> variance = atan2(sumsin,sumcos)
    
    variance =
    
        0.2074