+ Post New Thread
Results 1 to 4 of 4
  1. #1
    Advanced Member level 5
    Points: 17,282, Level: 31
    pancho_hideboo's Avatar
    Join Date
    Oct 2006
    Location
    Real Homeless
    Posts
    2,664
    Helped
    715 / 715
    Points
    17,282
    Level
    31

    Degeneration of Array in MATLAB

    We can get the completely same values in Array but with no repetitions
    by using "unique()" in MATLAB.
    Code:
    >> A=[1, 1, 2, 3, 4]
    
    A =
    
         1     1     2     3     4
    
    >> unique(A)
    
    ans =
    
         1     2     3     4
    Here I would like to relax an euqality condition.
    For example, I want to treat 1.0 and 1.001 as same value.
    However we can not set tolerance in "unique()".

    How can I degenarate Array with finite tolerance in MATLAB ?
    Code:
    >> A=[1.0, 1.001, 2, 3, 4]
    
    A =
    
        1.0000    1.0010    2.0000    3.0000    4.0000
    
    >> unique(A)
    
    ans =
    
        1.0000    1.0010    2.0000    3.0000    4.0000
    
    >>

    •   AltAdvertisement

        
       

  2. #2
    Advanced Member level 4
    Points: 7,568, Level: 20
    Achievements:
    7 years registered

    Join Date
    Jul 2010
    Location
    Sweden
    Posts
    1,013
    Helped
    387 / 387
    Points
    7,568
    Level
    20

    Re: Degeneration of Array in MATLAB

    You mean rounding?
    Code:
    octave> A=[1.0, 1.001, 2, 3, 4]
    A =
    
       1.0000   1.0010   2.0000   3.0000   4.0000
    
    octave> unique(round(A))
    ans =
    
       1   2   3   4
    According to the Matlab manual, you should be able to round to N decimal places by "round(A,N)" but that doesn't work in octave.
    To round to 2 decimals in octave, we can do (should also work fine in Matlab)
    Code:
    octave> A=[1.237, 1.242, 2, 3, 4]
    A =
    
       1.2370   1.2420   2.0000   3.0000   4.0000
    
    octave> unique(round(100*A)/100)
    ans =
    
       1.2400   2.0000   3.0000   4.0000



    •   AltAdvertisement

        
       

  3. #3
    Advanced Member level 5
    Points: 17,282, Level: 31
    pancho_hideboo's Avatar
    Join Date
    Oct 2006
    Location
    Real Homeless
    Posts
    2,664
    Helped
    715 / 715
    Points
    17,282
    Level
    31

    Re: Degeneration of Array in MATLAB

    Quote Originally Posted by std_match View Post
    You mean rounding?
    No.
    I want to specify relative tolerance.



    •   AltAdvertisement

        
       

  4. #4
    Advanced Member level 5
    Points: 17,282, Level: 31
    pancho_hideboo's Avatar
    Join Date
    Oct 2006
    Location
    Real Homeless
    Posts
    2,664
    Helped
    715 / 715
    Points
    17,282
    Level
    31

    Re: Degeneration of Array in MATLAB

    Code:
    function res = main
    clear all, close all, clc
    
    tol = 1e-2;
    A = [2.0001, -0.001, 1.0001, 1.0, 1.001, 0.0, 2.001, 2, 3.001, 3];
    
    A.'
    unique(A).'
    my_unique(A, [], tol).'
    my_unique(A(end:-1:1), [], tol).'
    my_unique(sort(A), [], tol).'
    
    A(1) = 8.6745e-016 + j*1.92803e-008;
    A(2) = 8.6746e-016 + j*-1.92804e-008;
    A(3) = -5.0306e-016 + j*0.68222;
    A(4) = -5.0307e-016 + j*-0.68223;
    A(5) = -5.0308e-016 + j*0.68224;
    A(6) = -5.0309e-016 + j*-0.68225;
    A(7) = 7.11236e-016 + j*0.970492;
    A(8) = 7.11237e-016 + j*-0.970493;
    A(9) = 7.11238e-016 + j*0.970494;
    A(10) = 7.11239e-016 + j*-0.970495;
    
    A.'
    unique(A).'
    my_unique(A).'
    my_unique( A(end:-1:1) ).'
    my_unique( sort(A) ).'
    
    function B = my_unique(A, abs_tol, rel_tol)
    if nargin <=1
        abs_tol = NaN;
        rel_tol = NaN;
    end
    if nargin <=2
        rel_tol = NaN;
    end
    
    if isempty(abs_tol), abs_tol = NaN; end
    if isempty(rel_tol), rel_tol = NaN; end
    
    if isnan(abs_tol) || abs_tol < 0.0, abs_tol = 1e-2; end
    if isnan(rel_tol) || rel_tol < 0.0, rel_tol = 1e-4; end
    
    i = 1;
    while i <= length(A)
        x0 = A(i);
        k = length(A);
        while k >= i + 1
            x1 = A(k);
            if abs(x0) > abs_tol
                d = abs(x1 - x0) / abs(x0);
                if d < rel_tol, A(k) = []; end
            else
                d = abs(x1 - x0);
                if d < abs_tol, A(k) = []; end
            end
            k = k - 1;
        end
        i = i + 1;
    end
    B = A;
    Code:
    ans =
    
        2.0001
       -0.0010
        1.0001
        1.0000
        1.0010
             0
        2.0010
        2.0000
        3.0010
        3.0000
    
    
    ans =
    
       -0.0010
             0
        1.0000
        1.0001
        1.0010
        2.0000
        2.0001
        2.0010
        3.0000
        3.0010
    
    
    ans =
    
        2.0001
       -0.0010
        1.0001
        3.0010
    
    
    ans =
    
        3.0000
        2.0000
             0
        1.0010
    
    
    ans =
    
       -0.0010
        1.0000
        2.0000
        3.0000
    
    
    ans =
    
       0.0000 + 0.0000i
       0.0000 - 0.0000i
      -0.0000 + 0.6822i
      -0.0000 - 0.6822i
      -0.0000 + 0.6822i
      -0.0000 - 0.6823i
       0.0000 + 0.9705i
       0.0000 - 0.9705i
       0.0000 + 0.9705i
       0.0000 - 0.9705i
    
    
    ans =
    
       0.0000 + 0.0000i
       0.0000 - 0.0000i
      -0.0000 + 0.6822i
      -0.0000 - 0.6822i
      -0.0000 + 0.6822i
      -0.0000 - 0.6823i
       0.0000 + 0.9705i
       0.0000 - 0.9705i
       0.0000 + 0.9705i
       0.0000 - 0.9705i
    
    
    ans =
    
       0.0000 + 0.0000i
      -0.0000 + 0.6822i
      -0.0000 - 0.6822i
       0.0000 + 0.9705i
       0.0000 - 0.9705i
    
    
    ans =
    
       0.0000 - 0.9705i
       0.0000 + 0.9705i
      -0.0000 - 0.6823i
      -0.0000 + 0.6822i
       0.0000 - 0.0000i
    
    
    ans =
    
       0.0000 + 0.0000i
      -0.0000 + 0.6822i
      -0.0000 - 0.6822i
       0.0000 + 0.9705i
       0.0000 - 0.9705i
    
    >>



--[[ ]]--