How to find unique

2 次查看(过去 30 天)
regina
regina 2015-10-17
编辑: regina 2015-10-18
I have two columns of numbers, X and Y, and i need to find each unique combination of numbers, so far i have this code
s=0
for i=1:n;
if length(unique(x(i)))==x(i);
s=s+length(unique(y(i)));
else if length(unique(y(i)))==y(i);
s=s+length(unique(x(i)));
end
end
end
I'm getting closer to my answer, so hopefully it just needs a few small tweaks, but i can't figure out what?

回答(1 个)

Walter Roberson
Walter Roberson 2015-10-17
unique([x,y],'rows')
  1 个评论
regina
regina 2015-10-18
编辑:regina 2015-10-18
okay, so i've defined
date=unique([x, y], 'rows');
outside of my for loop, where x and y are previously defined as the two different columns, then my for loop:
x=0
for i=1:ngames;
if 2003==year(i) & numel(date);
x=x+1;
end
end
where ngames=size(tab,1);
year=another column
but this is giving me the number of rows in 2003, but instead i want the number of unique dates in 2003
EDIT:wait actually, on the left hand side in the workspace tab, a table of variables has been created called date 192x2, and when i open this they are the values i want, how do i get it so it prints the answer 192? Thanks
EDIT 2:okay, solved this now by using length(date)
thank you for pointing me in the right direction!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by