# 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 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.

the cyclist on 12 Jul 2019
Upload your code here, and maybe some folks will give you some hints.
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)
end
Sai Swaroop Maram on 30 Jul 2020
function numfreeze=freezing(T)
T(T<32)=1;
T(T>=32)=0;
numfreeze=sum(T);

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

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.
Walter Roberson on 11 May 2020
Agamjyot is correct.
Hari Kiran Tirumaladasu on 5 Jun 2020
Tahsin Hossain,
Try this code,
function numfreeze = freezing(A)
n = (A<32);
numfreeze = sum(n);

Vineet Singhal on 14 Oct 2019
function numfreeze = freezing(v)
a= length(v(v<32));
numfreeze =a;
end

#### 1 Comment

Lokesh Sahu on 30 Aug 2020 at 15:07
function numfreez = freezing(x)
numfreez = length (x (x<32)) ;
end
%you dont even need that a, just use numfreez directly

mohammad elyoussef on 4 Apr 2020
function b = freezing(a)
f = a < 32;
b = sum(f);

Yash Agarwal on 22 Apr 2020
function numfreeze = freezing(A)
B = A(A<32);
numfreeze = size(B,2);
end

#### 1 Comment

Rik on 13 Aug 2020
You are not promissed A is a either a row or column vector, so your solution should support both input types.

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

Wrong. This would be more compact:
freezing=@(x)length(x(x<32));
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.