Wrong answer from using UNIQUE function
3 views (last 30 days)
Show older comments
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; endendunique(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
Accepted Answer
Star Strider
on 12 May 2014
Edited: Star Strider
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');
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!