Vector with if condition

9 views (last 30 days)
Klaudio Myrtaj
Klaudio Myrtaj on 27 Mar 2020
Commented: Klaudio Myrtaj on 27 Mar 2020
Hi everyone,
I got stuck while trying to create a nx1 vector from a nxm matrix.
The matrix is created by two inputs, T01(n,1) and RH(1,m). The result is Td(n,m).
When an element of each row is bigger than an other parameter (Ts2), I have to pick up the value of RH.
I created this code, but it isn't working. (I'm writing here the entire code and I know it's a little bit long)
It returns a vector with all hundrends or zeros .
Could anyone help me please ?
Thank you !
function [c] = cond_humidity1 (T01, RH, c2)
n=length(T01); %size of the martixes
m=length(RH);
rho=zeros(n,1); % Allocation of some vectors
Ps2=zeros(n,1);
Ts2=zeros(n,1);
Pv_sat=zeros(n,1);
Pv=zeros(n,m);
Pv2=zeros(n,m);
Td=zeros(n,m);
T_wall=zeros(n,1); %This is the vector who's values has to be compared with Td(i,j) values
for i=1:n
rho(i) = 28.96/(0.0821*(T01(i) +273.16));
Ps2(i) = 101300 - ((c2^2)/2)*rho(i);
Ts2(i) = T01(i)-(c2^2)/2060;
T_wall(i) = Ts2(i) + 0.8*(T01(i)-Ts2(i));
Pv_sat(i) = 6.1 *10^((7.4*T01(i))/(T01(i)+240.73));
for j=1:m
Pv(i,j) = Pv_sat(i) *(RH(j)/100);
Pv2(i,j) = (Ps2(i)/101300)*Pv(i,j);
Td(i,j) = 230.73/((7.4/log10(Pv2(i,j)/6.1))-1); %Till here everything works well
if Td(i,j)>=T_wall(i) % and I checked that there exists values
c(i)=RH(j); % Td(i,j) higher than T_wall
end
end
end
end
It returns a vector with all values 100 or 0
  4 Comments
darova
darova on 27 Mar 2020
I tried this case
n = 10;
m = 15;
T01 = -rand(1,n)*350;
RH = rand(1,m);
c2 = 1;
and get
c'
ans =
Columns 1 through 5
0 0 -202.1897 -164.6581 -230.7300
Columns 6 through 10
-230.7300 -214.6403 0 0 0
Klaudio Myrtaj
Klaudio Myrtaj on 27 Mar 2020
Edited: Klaudio Myrtaj on 27 Mar 2020
I would also modify the last line: c(i) = RH(j); ( not c(i)= Td(i,j)
Already changed the code above

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 27 Mar 2020
Klaudio - so with your sample inputs and changing the last line assignment to c(i) = RH(j); then I see (like you) that all elements of c are set to 100. In this code
for i=1:n
% snip!
for j=1:m
% snip
Td(i,j) = 230.73/((7.4/log10(Pv2(i,j)/6.1))-1);
if Td(i,j)>=T_wall(i)
c(i)=RH(j);
end
end
end
note how we may update c(i) with RH(j) for different j. Is this correct? Because it seems that once we find one j that satisfies Td(i,j) >= T_wall(i), then all subsequent j satisify this too...and so we continue to update c(i) with different RH(j) until we reach the last element of RH which is 100. So do you want to do this? Or do you want jump or break out of this inner for loop upon the first update to c(i) like
if Td(i,j)>=T_wall(i)
c(i)=RH(j);
break;
end
?
  1 Comment
Klaudio Myrtaj
Klaudio Myrtaj on 27 Mar 2020
Geoff, thank you so much. Seems like it's working now. I had to put "break" because I need just the first value of RH(j) that satisfies the relation Td(i,j) = T_wall(i). Than just keep visiting the other rows to do the same operation. Your help was great, thank you !

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!