MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by jiahua
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.

*No products are associated with this question.*

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

[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');

## 0 Comments