필터 지우기
필터 지우기

counting the number of data in the csv file

조회 수: 3 (최근 30일)
Ireedui Ganzorig
Ireedui Ganzorig 2020년 3월 16일
댓글: Image Analyst 2020년 3월 16일
Hello MATLAB community
temps is a bunch of numbers ranging from 20 to 70 in a column. So what I am struggling to do is that I want to sum the number of data points that are below 32 (just the number of data points not the actual data, which are numbers ranging from 20 to 70, summed up). Below is my code and I just don't know how to continue.
temps = load('40819DecJanLancasterTemp.csv');
plot(temps)
xlabel('Dec2018 Jan2018 Dec2019 Jan2019')
ylabel('Temperature (F)')
title('Hourly Temperature')
for i = 1:length(temps)
if temps(i) <= 32
elseif temps(i) > 32
end
end

채택된 답변

Image Analyst
Image Analyst 2020년 3월 16일
Try this
indexesBelow32 = temps < 32
countBelow32 = sum(indexesBelow32)
You could of course do it all in one shot:
countBelow32 = sum(temps < 32)
No for loop needed to do the count.
  댓글 수: 2
Ireedui Ganzorig
Ireedui Ganzorig 2020년 3월 16일
Thank you very much Image Anaylyst. I literally spent solid 4 hours trying to figure it out. I knew I had to use the built-in sum command but just didn't think the inequality signs go inside the parenthesis. Again Thank you.
Image Analyst
Image Analyst 2020년 3월 16일
Yes, the trick is to understand the difference between logical indexes and linear indexes.
Logical indexes are either true or false (1 or 0) if the condition is met. So we can just sum those to get a count.
Linear indexes are the actual indexes, like 1,2,3,etc. So like if we had
temps = [1,99, 2, 88, 3, 77]
logicalIndexes = temps < 32
then logicalIndexes would be [1, 0, 1, 0, 1, 0]. If we sum that we get 3.
Now the linear indexes
linearIndexes = find(logicalIndexes) % Or, equivalently find(temps < 32)
would be [1, 3, 5], which is a list of only where the logical indexes are "true" (meaning they're 1).
To get the actual numbers from the array, rather than their indexes/locations, you can use either the logical or linear indexes.
smallNumbers = temps(logicalIndexes)
smallNumbers = temps(linearIndexes)
smallNumbers would be [1,2,3] no matter which line of code above that you used. This is one of the wonderful things about a vectorized language like MATLAB.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by