How to use the unique command in a loop and how to filter data?

조회 수: 2 (최근 30일)
regina
regina 2015년 10월 21일
답변: TastyPastry 2015년 10월 21일
I have a table of data for the year 2001, i need goals less than or equal to 12 and then to count the number of different teams that only fit in this category (have never scored higher than 12 goals in 2001), so far:
t= length(unique(guest_team(i)));
y=0
for i=1:games;
if 2001==year(i) & t & 12>=guest_goals(i) ;
y=y+1;
end
end
however, this just gives me the number of instances any team scores less than or equal to 12, any help would be greatly appreciated thanks!

답변 (1개)

TastyPastry
TastyPastry 2015년 10월 21일
If your data is organized something like:
Year Team Goals
2001 1 14
2001 2 12
2002 3 8
2003 1 9
2002 1 16
...
Where Year, Team and Score are separate vectors but each index corresponds to a game, then
numTeams = numel(unique(guest_team(year == 2001 && guest_goals <= 12)));
You're looking for games in 2001, goals less than 12. That creates your mask for guest_team. Then, the output of unique() will only find one instance of each team, numel() finds how many teams there are.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by