## Wrong answer from using UNIQUE function

### jiahua (view profile)

on 12 May 2014
Latest activity Commented on by Star Strider

on 12 May 2014

### Star Strider (view profile)

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.

## Products

No products are associated with this question.

### Star Strider (view profile)

on 12 May 2014
Edited by Star Strider

### Star Strider (view profile)

on 12 May 2014

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

jiahua

### jiahua (view profile)

on 12 May 2014

Thank you! It works.

Star Strider

### Star Strider (view profile)

on 12 May 2014

My pleasure!

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