Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Degeneration of Array in MATLAB

Status
Not open for further replies.

pancho_hideboo

Advanced Member level 5
Joined
Oct 21, 2006
Messages
2,847
Helped
767
Reputation
1,536
Reaction score
732
Trophy points
1,393
Location
Real Homeless
Activity points
17,490
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

>>
 

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
 

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

>>
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top