how to use if-else for select transition matrix input

d = 1,2,5,10 will use tm = p_clear but d = 3,4,7 will use tm = p_partly and d = 6,8,9 will use tm = p_cloudy. what command that i should use? The below is code that i wrote but it has problem about transition matrix that used not match with day. pleas help me.
if d == 1 || d == 2 || d == 3 | d == 4 || d == 5 || d == 7 || d == 17;
p = cdf_partly;
elseif d == 6 || d == 8 || d == 9 || d == 10;
p = cdf_mostly;
end

 Accepted Answer

Hi,
I do not completely understand your problem, but you can write your conditions with a switch-case statement:
function p = test2(y)
switch y
case {1, 2, 5, 10};
p=0;
case {3, 4, 7};
p=1;
case {6, 8, 9};
p=2;
otherwise
p=-1;
end
end

6 Comments

Thank you for your answer, but i did same you done, it's not work or i have some mistake. Please suggest to me again. This is my script>>>
seq = size(day,1);
for step = 1:seq;
d = day(step,1);
switch d
case{1,2,3,4,5,7,17,18,20,21,23,24,29,31};
p = cdf_partly;
case{6,8,9,10,11,13,14,15,16,19,22,25,26,27,28,30};
p = cdf_mostly;
case{12};
p = cdf_cloudy;
otherwise;
p = cdf_clear;
end
What mistake do you have? At the moment you overwrite p in each loop iteration. I suggest that you use an array and write p(step).
sorry, please you show example that you said. I cannot understand. Thank you very much.
What exactly do you need? If you get an error message, please let us know what the message is and in which line it occurs. I notice that your code misses an "end" to close the for loop. Is that the problem?
I have file in worksheet its name "day". In this file is array of number [1; 1; 1; 2; 2; 2; 3; 3; 3; 4; 4; 4....,10]. From my code, i define "d" is value that read from day file in each row. That mean d=1,d=1,d=1,d=2,d=2,d=2...] and i create loop if d==1, i will determine p = cdf_clear(cdf_clear is matrix 10*10) but d==2, i will determine p=cdf_parlty(it not same cdf_clear). The result that i want is
output file : @ day 1 >> 0.34 [value that was got from *cdf_clear* (i,j)]
1 >> 0.85 [value that was got from *cdf_clear* (i,j)]
......
2 >> 0.23 [value that was got from *cdf_partly* (i,j)]
.....
3 >> 0.26 [value that was got from *cdf_mostสั* (i,j)]
Are you understand with i explain?. I used if-else
if d==1 || d==3 || d==7;
p = cdf_clear;
elseif d==4 || d== 5;
p = cdf_partly;
end.
It not work. I'm beginner, i try to watch technique from internet but i don't know keyword for thing that i want. Thank you every much.
Ok, but what is the problem with Julia's answer? If you replace p=0 with p=cdf_clear and so on, it should work.

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!