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

255 views (last 30 days)
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.
##### 3 CommentsShowHide 2 older comments
Sai Swaroop Maram on 30 Jul 2020
function numfreeze=freezing(T)
T(T<32)=1;
T(T>=32)=0;
numfreeze=sum(T);

Sign in to comment.

### Accepted Answer

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
##### 6 CommentsShowHide 5 older comments
Hari Kiran Tirumaladasu on 5 Jun 2020
Tahsin Hossain,
Try this code,
function numfreeze = freezing(A)
n = (A<32);
numfreeze = sum(n);

Sign in to comment.

### More Answers (5)

Vineet Singhal on 14 Oct 2019
function numfreeze = freezing(v)
a= length(v(v<32));
numfreeze =a;
end
##### 2 CommentsShowHide 1 older comment
it is working thanks

Sign in to comment.

mohammad elyoussef on 4 Apr 2020
function b = freezing(a)
f = a < 32;
b = sum(f);
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

Yash Agarwal on 22 Apr 2020
function numfreeze = freezing(A)
B = A(A<32);
numfreeze = size(B,2);
end
##### 1 CommentShowHide None
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.

Sign in to comment.

Rajeev Mehndiratta on 28 Oct 2020
function numfreeze = freezing(V)
A=0
V
V(V<32) = A
A =logical(V)
B=sum(A)
numfreeze=B
end
##### 1 CommentShowHide None
Rik on 28 Oct 2020
You're correct, this solution didn't exist in this thread yet, although I would clean it up to this:
function numfreeze = freezing(V)
V(V<32) = 0;
V = logical(V);
numfreeze = sum(V);
end

Sign in to comment.

Nadeem U Rehman on 10 Dec 2020
function numfreeze = freezing(A)
F = A(A<32);
[row column] = size(A);
if size(A) == [1 column]
numfreeze = size(F,2);
else
numfreeze = size(F,1);
end
end
##### 4 CommentsShowHide 3 older comments
Rik on 10 Dec 2020
These homework solutions are probably not the best place to get feedback. After completing the Onramp tutorial (which is provided for free by Mathworks), I would suggest looking at this thread.
And where you went wrong is in assuming what == does, instead of reading the documentation. There is an important difference between equals (which is called when you write ==) and isequal. size(A)==[row column] will result in a two-element logical vector if A is a vector or 2D array (and an error if A has more dimensions).
% Let's take a look at what if does:
A=[]; if A, disp(A),end
A=true; if A, disp(A),end
1
A=[true false]; if A, disp(A),end
A=[true true]; if A, disp(A),end
1 1
A=[false true]; if A, disp(A),end
A=[false false];if A, disp(A),end
Did you expect these results? What does this say about how your code would work?

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!