How can i count how many times does a temperature goes through in a loop

1 回表示 (過去 30 日間)
Angel Sanchez
Angel Sanchez 2020 年 2 月 12 日
コメント済み: Angel Sanchez 2020 年 2 月 12 日
For example if I have an array of temperatures called temps=[30, 45, 60, 15, 70, 25], and i want to figure out how many temepartures are below 32 and save that number how would i do that. For sure i would need to get 2 because there only 2 temeparatures that are below 32. Would i need to use a while loop.

回答 (2 件)

Akhila Bonagiri
Akhila Bonagiri 2020 年 2 月 12 日
The below code would work for you. Here we find the indices of the temperature values which are less than 32 and count them. Which would give your required answer.
>> Num = numel(find(temps<32));

fred  ssemwogerere
fred ssemwogerere 2020 年 2 月 12 日
A for loop may not be necessary. you could use lt
temps=[30, 45, 60, 15, 70, 25];
val32=temps(lt(temps,32)); % this will return an array of temperatures less than "32".
% To know the number of tempaeratures less than "32", you could use the line of code below which counts the number of "true"(1) observations
numVal32=nnz(lt(temps,32));
  1 件のコメント
Angel Sanchez
Angel Sanchez 2020 年 2 月 12 日
Okay but If I want to use a while loop for this how would that work.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by