No announcement yet.

vector coding

  • Filter
  • Time
  • Show
Clear All
new posts

  • vector coding

    I am trying to analyze two segment angles using vector coding (as described in Needham et al. J of Biomechanics 2014). I get an error then it comes to the atan2 function. Below is my current code. Can you please offer any suggestions.

    function [Coupling_angle, mean_coupling_angle, CP_SD]= mean_coupling_angle(Array1,Array2);

    [r1 c1] = size (Array1);

    for i = 1:r1-1
    x(i,: )=Array1(i+1,1)-Array1(i,1);
    y(i,: )=Array2(i+1,1)-Array2(i,1);
    ang(i,: )=y(i,: )/x(i,: );

    % convert degrees to radians
    angRad = deg2rad(ang);
    CP_ang(i,: )=atan2(ang(i,: ));

    % convert coupling angle in radians to degrees
    CP_ang = rad2deg(CP_ang)

  • #2
    Re: vector coding

    I have also tried to create the codes in R, from the Needham paper. I have attached it. You can change the file extension to ".R" if you want to use in R. I simply used atan, and went about the long winded way to copy the exact formula. I think one error is that under atan2(y,x), you need to input individual numbers, but instead you input one, which is the ratio of y/x. if you put atan2(y(i,: ),x(i,: )) (for example, I am not well versed with programming), that may help.

    PS: I am not a programmer, and sorry if my codes are clunky. I like yours, and I think I can tidy up mine from yours. Feel free to edit and notify me if there are errors you noticed.

    Attached Files


    • #3
      Re: vector coding


      it is quite reasonable to have an error in Matlab (but also in most programming languages) when trying to execute the atan2 function with single argument. Although atan2 function can be programmatically used to solve the mathematical equation θ = tan-1 (y/x), the computer implementation of the function is a little bit different than the equation. In contrast to the single-argument atan function, which depends only on the trigonometric ratio (y/x), the atan2 function, and as the name implies, requires two arguments to function properly. The two arguments are the coordinates of the point (x, y) which should be given in reverse when using Matlab, i.e. atan2(y, x).

      It should also be noted that the parameters given to the atan2 function are treated as Cartesian coordinates and not as angles; even when they are originating from the angle-angle plot. Thus, there is no need to convert these values from degrees to radians before passing them to the function.

      It is, however, noteworthy that atan2 function returns the angle in radians. Since coupling angles are better expressed in degrees, I would suggest using the atan2d function to directly calculate the angle in degrees, rather than using a conversion factor. This will not only enhance the readability of the code but also the execution time.

      Lastly, atan2d function returns the angles in the closed interval [-180°, 180°]. For the coupling angle to be in the range between 0° and 360°, a value of 360 must be added to negative angle values. This can be done be using conditioned statements or by using the modulo operation.

      Taken altogether, the code in Matlab for computing the coupling angles - according to the vector coding method - can be reduced to the following sentence:

      Coupling_angle = mod(atan2d(diff(Array2), diff(Array1)), 360);

      where Array1 and Array2 are the segmental angles represented on the x and y axis of the angle-angle diagram, respectively.

      Hope it helps and remain with
      Best regards,


      • #4
        Re: vector coding


        I have dabbled in vector coding. See below for code I dug up and reference. The last for loop could be eliminated with a more efficient logic test, but I am too lazy to change it.

        % this program computes the phase angle between two joint/segment angles joint1 and joint2 (e.g. 100 pt data)
        function [phsang] = VectCodeTrials(joint1,joint2)

        % find the difference between each point in time
        % this will leave you with one less point than the entry matrix

        % find the vector magnitude between each xd-yd pair
        % use ./ to do the operation on each pt instantly
        vmag =sqrt(xd.^2+yd.^2);

        % cosine and sine for each xd yd pair

        %find the phase angle for all 99 points
        phsang=atan2(sinang, cosang)*(180/pi);

        for j = 1:r
        if phsang(j,1) < 0
        phsang(j,1) = phsang(j,1) + 360;

        Chang, R., Van Emmerik, R., Hamill, J., 2008. Quantifying rearfoot-forefoot coordination in human walking. J. Biomech. 41 (14), 3101–3105.