Using Length and Find
3 views (last 30 days)
Show older comments
I am trying to find the sum of numbers in a matrix by the 4th inning (the data is from multiple softball games) so that I can compare them to see who is in the lead by the 4th inning. I have tried the code below and cannot figure out how to get it to work. The two that aren't working are the oppPtsAfter4 and auPtsAfter4
%Auburn record after all games
auNumWins = length(find(gamePts(:,1)<gamePts(:,2)));
fprintf('Auburn season record: %d-%d\n',auNumWins,auRows-auNumWins)
%Auburn record at the end of the 4th inning
oppPtsAfter4 = length(find(oppPoints,4:8));
auPtsAfter4 = length(find(auPoints,13:17));
0 Comments
Answers (1)
Walter Roberson
on 25 Jul 2020
When you use a numeric second parameter for find(), then you are controlling the number of reponses you want returned. It does not make sense to put a vector at that point.
Wouldn't you be wanting to look at something like oppPoints(:,4) and auPoints(:,4) ?
auNumWins = length(find(gamePts(:,1)<gamePts(:,2)));
A much more compact way of writing that is
auNumWins = nnz(gamePoints(:,1) < gamePoints(:,2));
3 Comments
Walter Roberson
on 25 Jul 2020
Is the matrix a list of points per inning? If so then sum(oppPoints(:,1:4), 2) would give the sum for the first 4 innings.
auPoints,13:17
Is your data file set up so that for each game you have a list of 9 values that are opponent runs in one inning, followed by a list of 9 values that are Auburn runs in one inning? If so then it could make sense to access columns 1:4 (the opponent) and 10 to 13 (auburn). However if the data has already been split into two matrices, as hinted by oppPoints and auPoints being different arrays, then it would not appear to make sense to look in columns 10:13 of auburn data.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!