How to find unique

2 visualizzazioni (ultimi 30 giorni)
regina
regina il 17 Ott 2015
Modificato: regina il 18 Ott 2015
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?

Risposte (1)

Walter Roberson
Walter Roberson il 17 Ott 2015
unique([x,y],'rows')
  1 Commento
regina
regina il 18 Ott 2015
Modificato: regina il 18 Ott 2015
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!

Accedi per commentare.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by