How to apply if else statement by reading values from a Table file on each value and save output?
1 view (last 30 days)
Show older comments
So, I have this code, This is working fine when only one value of Turbidity is given, it successfully gives me a output of TurbidityNEW.
num_col = size(Turbidity);
for i=1:num_col
if Turbidity < 5 % Condition 1
TurbidityNEW = 1;
elseif Turbidity >=5 & Turbidity <= 10 % Condition 2
TurbidityNEW = Turbidity/5;
elseif Turbidity > 10 & Turbidity <= 500
TurbidityNEW = (Turbidity+43.9)/34.5; % Condition 3
end
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/673048/image.png)
But, I need, this code to read multiple values of Turbidity from a Table(or double), then find where it lies, in condition 1 or 2 or 3, & then run the specific statement and give me the TurbidityNEW values in a table (or double).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/673053/image.png)
0 Comments
Accepted Answer
Yazan
on 3 Jul 2021
% generate values randomly between 0 and 500 for an example
a = 0;
b = 500;
Turbidity = (b-a).*rand(1000,1) + a;
TurbidityNEW = nan(size(Turbidity));
% to save the index of the satisfied condition (1, 2, or 3)
satCondition = nan(size(Turbidity));
num_col = size(Turbidity);
%%% your code modified %%%
for i=1:num_col
cond = nan;
if Turbidity(i) < 5 % Condition 1
TurbidityNEW(i) = 1;
cond = 1;
elseif Turbidity(i) >=5 && Turbidity(i) <= 10 % Condition 2
TurbidityNEW(i) = Turbidity(i)/5;
cond = 2;
elseif Turbidity(i) > 10 && Turbidity(i) <= 500 % Condition 3
TurbidityNEW(i) = (Turbidity(i)+43.9)/34.5;
cond = 3;
end
satCondition(i) = cond;
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Logical 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!