How to apply if else statement by reading values from a Table file on each value and save output?
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Yazan
2021-7-3
% 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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!