Error in if condition

2 Ansichten (letzte 30 Tage)
Ararat Cetinkaya
Ararat Cetinkaya am 24 Mär. 2020
Beantwortet: Sriram Tadavarty am 24 Mär. 2020
hi everybody,
I have a two different variables; enthleft and enthright. I want to turn NaN values to Zero but when both variables get NaN values just one of it turn to Zero. How to solve this porblem?
if any(isnan(enthleft), 'all');
enthleft = 0;
elseif any(isnan(enthright), 'all');
enthright = 0;
end

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 24 Mär. 2020
Ararat - don't use the elseif and just use two if statements (since you want to apply a specific action to both variables)
if any(isnan(enthleft), 'all');
enthleft = 0;
end
if any(isnan(enthright), 'all');
enthright = 0;
end
By the way, your above code will set all elements of the enthleft or enthright arrays to zero if at least one element is NaN. Is this the intended behaviour?

Weitere Antworten (1)

Sriram Tadavarty
Sriram Tadavarty am 24 Mär. 2020
Hi Ararat,
Based on the information provided, the following will help
if any(isnan(enthleft), 'all') && any(isnan(enthright), 'all')
enthleft = 0;
enthright = 0;
end
Hope this helps.
Regards,
Sriram

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by