MATLAB Answers

jiahua

Wrong answer from using UNIQUE function

Asked by jiahua
on 12 May 2014
Latest activity Commented on by Star Strider
on 12 May 2014

I use the following codes to get a row vector and then unique it. But the answer is wrong.

  • _v=3; alpha=0.5; i=1; for a=0:0.1:v for b=0:0.1:3-a x1=a; x2=b; x3=3-a-b; u(i)=x1-alpha*(x2-x1)-alpha*(x3-x1); i=i+1; end end unique(u)

Following is part of the answer:

-1.50000000000000	-1.25000000000000	-1.25000000000000	-1	-1.00000000000000	-1.00000000000000	-0.750000000000000	-0.750000000000000	-0.750000000000000	-0.750000000000000   ...

The result is definitely wrong. But I cannot find where the problem is. I'm a little crazy...

Anyone helps me is greatly appreciated.

  0 Comments

Products

No products are associated with this question.

1 Answer

Answer by Star Strider
on 12 May 2014
Edited by Star Strider
on 12 May 2014
 Accepted answer

If that was how you unique, you did not call it correctly. If you want to know everything about the unique rows of u, call it as:

[C,ia,ic] = unique(u,'rows');

The documentation explains the three outputs and how to use them.

Your vector u turns out to be a row vector, so to call unique using the 'rows' argument, transpose it:

v=3; 
alpha=0.5; 
i=1; 
for a=0:0.1:v 
    for b=0:0.1:3-a 
        x1=a; 
        x2=b; 
        x3=3-a-b; 
        u(i)=x1-alpha*(x2-x1)-alpha*(x3-x1); 
        i=i+1; 
    end 
end
[C,ia,ic] = unique(u','rows');

  2 Comments

jiahua
on 12 May 2014

Thank you! It works.

Star Strider
on 12 May 2014

My pleasure!


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today