How to use if statements with arrays?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Dylan Zeigler
am 12 Feb. 2013
Bearbeitet: David
am 28 Jan. 2020
I am trying to use an if statement with an array,
x = 1:1:4
if x > 2
y = x + 1
else
y = x - 1
end
I want the resulting array for y to be [ 0, 1, 4, 5], how would i execute specific functions for only certain numbers in an array? i want y = x + 1 to only affect the numbers in the x array that are greater then 2 and y = x - 1 to only affect the numbers in the x array that are less then 2.
0 Kommentare
Akzeptierte Antwort
Brian B
am 12 Feb. 2013
y = (x>2).*(x+1) + (x<=2).*(x-1)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!