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
135 views (last 30 days)
Show older comments
Andrew Marttini
on 12 Jul 2019
Commented: John D'Errico
on 25 Apr 2023
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 Comments
Sai Swaroop Maram
on 30 Jul 2020
function numfreeze=freezing(T)
T(T<32)=1;
T(T>=32)=0;
numfreeze=sum(T);
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
7 Comments
SOUMYAJIT MONDAL
on 29 Jul 2021
Hari Kiran
Your ans will come wrong.
numfrezee will sum the elements less than 32
so it will be 21+32+....
More Answers (6)
Vineet Singhal
on 14 Oct 2019
function numfreeze = freezing(v)
a= length(v(v<32));
numfreeze =a;
end
2 Comments
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 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
A=[true false]; if A, disp(A),end
A=[true true]; if A, disp(A),end
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?
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.
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 Comment
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
Mohamed
on 25 Apr 2023
well, you can use the size function to determine the number of the aviable elements:
function output= freezing(x)
x_1=x(x<32); % it will output any number less than 32 of the given matrix
[b,output=size(x_1); % b will equal to the unwanted elements (rows), output will equal to the number of the wanted number ( coloumbs)
%it can be vise versa if its a coloumb matrix%
1 Comment
John D'Errico
on 25 Apr 2023
This is identical to at least one other solution already posed, except for one problem. Your code will fail due to a syntax error.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!