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)
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

Sign in to comment.

Accepted Answer

Stephan
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

Sign in to comment.

More Answers (6)

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

Nadeem U Rehman
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
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.


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

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

Rajeev Mehndiratta
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
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.


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

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