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
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).

采纳的回答

Yazan
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 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by