how to use if statement with arrays

7 vues (au cours des 30 derniers jours)
Jordan Paxton
Jordan Paxton le 16 Oct 2019
Commenté : Stephen23 le 16 Oct 2019
I am trying to take a section of a 1D array and test if it would be less than a value and if it is the display error, The only thing I am getting back is the array itself.
code:
A=[11 9 17 16 29 41 42 65 88]
if A(1:4)>10
disp("error in hw catagory, please fix hw scores")
end
if A(5:6)>30
disp("error in quiz catagory, please fix quiz scores")
end
if A(7:8)>50
disp("error in midterm catagory, please fix midterm grades")
end
if A(9)>100
disp ("error in final catagory, please fix final scores")
end

Réponse acceptée

TA
TA le 16 Oct 2019
This might solve what you're trying to do:
if (mean(A(1:4)>10) > 0)
disp("error in hw catagory, please fix hw scores")
end
Using logical "A(1:4)>10" would tell you about whether any of the case contained within if statement is true or not.
  1 commentaire
Stephen23
Stephen23 le 16 Oct 2019
Simpler to use any:
if any(A(1:4)>10)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Software Development Tools dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by