Info

This question is locked. Riaprila per modificarla o per rispondere.

# 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

48 visualizzazioni (ultimi 30 giorni)
Andrew Marttini il 12 Lug 2019
Locked: Rik il 9 Lug 2024
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 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Andrew Marttini il 12 Lug 2019
Modificato: Andrew Marttini il 12 Lug 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 il 30 Lug 2020
function numfreeze=freezing(T)
T(T<32)=1;
T(T>=32)=0;
numfreeze=sum(T);

### Risposta accettata

Stephan il 12 Lug 2019
function numfreeze = freezing (n)
n1 = n(n<32)
numfreeze = numel(n1)
end
Dont overwrite n - it is an input argument
##### 7 CommentiMostra 5 commenti meno recentiNascondi 5 commenti meno recenti
Hari Kiran Tirumaladasu il 5 Giu 2020
Modificato: Hari Kiran Tirumaladasu il 5 Giu 2020
Tahsin Hossain,
Try this code,
function numfreeze = freezing(A)
n = (A<32);
numfreeze = sum(n);
SOUMYAJIT MONDAL il 29 Lug 2021
Hari Kiran
numfrezee will sum the elements less than 32
so it will be 21+32+....

### Più risposte (6)

Vineet Singhal il 14 Ott 2019
function numfreeze = freezing(v)
a= length(v(v<32));
numfreeze =a;
end
##### 2 CommentiMostra NessunoNascondi Nessuno
Lokesh Sahu il 30 Ago 2020
function numfreez = freezing(x)
numfreez = length (x (x<32)) ;
end
%you dont even need that a, just use numfreez directly
it is working thanks

Nadeem U Rehman il 10 Dic 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 CommentiMostra 2 commenti meno recentiNascondi 2 commenti meno recenti
Nadeem U Rehman il 10 Dic 2020
Modificato: Nadeem U Rehman il 10 Dic 2020
i get feed back from people that know better than me, helps in learning. i deleted the previous one because you helped in figuring out the limited application of my code. Thank you!
please, tell me where i went wrong in this code.
i am new to MATLAB/Coding!
Rik il 10 Dic 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?

mohammad elyoussef il 4 Apr 2020
function b = freezing(a)
f = a < 32;
b = sum(f);
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Yash Agarwal il 22 Apr 2020
function numfreeze = freezing(A)
B = A(A<32);
numfreeze = size(B,2);
end
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Rik il 13 Ago 2020
You are not promissed A is a either a row or column vector, so your solution should support both input types.

Rajeev Mehndiratta il 28 Ott 2020
function numfreeze = freezing(V)
A=0
V
V(V<32) = A
A =logical(V)
B=sum(A)
numfreeze=B
end
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Rik il 28 Ott 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 il 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%
##### 2 CommentiMostra NessunoNascondi Nessuno
John D'Errico il 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.
DGM il 19 Ago 2023
Modificato: DGM il 19 Ago 2023
What's more remarkable is that the answer which this most closely duplicates had already been explained to be problematic. If I recall, the deleted version would have been a more appropriate comparison, but don't trust my recollection too much. These threads are all starting to blend together.
To reiterate and address what's been said in other comments, the question does not specify the vector orientation. Any decent solution should work for any vector orientation. When I say any orientation, I mean any array with only one non-singleton dimension.
Furthermore; the question text does vaguely suggest the use of array inputs. Since there's no good reason that a solution can't be generalized to support any N-D numeric array, then it's my opinion that it should. Such a solution would also support any vector.
So what are the problems with this example? Besides the missing bracket, The misuse of size() will result in the function silently returning incorrect results for anything other than a vector oriented along dim2 or higher. The fact that this actually works for higher-dim vectors was almost certainly nothing more than an accident based on a common misunderstanding of how size() works.
x = randi([0 64],100,1); % a column vector
freezing(x) % wrong output
ans = 1
x = reshape(x,1,[]); % a row vector
freezing(x) % correct output
ans = 51
x = reshape(x,1,1,[]); % a vector on dim3
freezing(x) % output is accidentally still correct
ans = 51
x = reshape(x,10,10); % a matrix
freezing(x) % wrong output
ans = 1
function output = freezing(x)
x_1 = x(x<32);
[~, output] = size(x_1); % tilde works here since R2009b
end
You could fix the problem:
function output = freezing(x)
x_1 = x(x<32);
output = numel(x_1);
end
... but that answer has already been posted, and it can still be simplified further anyway. I should also point out that the (likely autograded) assignment clearly specifies what the output variable name should be.
function numfreeze = freezing(x)
numfreeze = nnz(x<32);
end
Note now that not only is this version simpler, it's also more generalized.

This question is locked.

### Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by