MATLAB Answers

Write a function called freezing that takes a vector of numbers that correspond to daily low temperatures in Fahrenheit. Return numfreeze, the number of days with sub freezing temperatures (that is, lower than 32 F) without using loops. Here is an ex

794 views (last 30 days)
Andrew Marttini
Andrew Marttini on 12 Jul 2019
Commented: Walter Roberson about 1 hour ago
This question is soft-locked: new answers that are equivalent to already posted answers may be deleted without prior notice.
Hello there, I am very new to Matlab and I am having trouble with this question. I understand how to make the function work for the given matrix in the problem. However, I cannot find out how to make it work for random temperature vectors. Would anyone mind giving me a hint or helping me out? Would be greatly appreciated. Thank you.


Andrew Marttini
Andrew Marttini on 12 Jul 2019
So this is what I have so far. Its mostly just the code to solve for the vector thats given.
function numfreeze = freezing (n)
n = [45 21 32 31 51 12]
n1 = n(n<32)
numfreeze = numel(n1)

Sign in to comment.

Accepted Answer

Stephan on 12 Jul 2019
function numfreeze = freezing (n)
n1 = n(n<32)
numfreeze = numel(n1)
Dont overwrite n - it is an input argument


Show 3 older comments
Agamjyot Singh Chadha
Agamjyot Singh Chadha on 11 May 2020
Using logical can create a bug. For example, if the array 'n' has a value of 0 in it. By using logical the value of the input 0 will be 0 in the array n when instead it should be 1, cause 0 is less than 32. Thus the final result after summing everything in n will get you the wrong answer.

Sign in to comment.

More Answers (4)

Jessica Trehan
Jessica Trehan about 2 hours ago
function numfreeze = freezing(x)
n = x(x<32);
numfreeze= length(n);
%The most accurate and compact code.


Rik about 2 hours ago
Wrong. This would be more compact:
A solution equivalent to yours was also already posted on this thread. Why should your answer not be deleted?
Walter Roberson
Walter Roberson about 1 hour ago
There are more compact representations than Rik's suggestion -- two of them the same length using slightly different function calls.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by