Effacer les filtres
Effacer les filtres

How to apply if else statement by reading values from a Table file on each value and save output?

3 vues (au cours des 30 derniers jours)
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).

Réponse acceptée

Yazan
Yazan le 3 Juil 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

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by