if greater than in for loop

63 次查看(过去 30 天)
using a for loop and if statement, i need to subtract 25 from each element in a vector called "seven" that is greater than 100.
here is my code, but when i run it i get the original values of all elements in seven. how can i fix this?
for k = seven
if k > 100
k - 25
else
k + 0
end
end

采纳的回答

KSSV
KSSV 2020-6-26
for k = 1:length(seven)
if seven(k) > 100
seven(k) = seven(k) - 25 ;
end
end

更多回答(1 个)

Rafael Hernandez-Walls
seven=(seven>100).*(seven-25)+(seven<=100).*seven;

类别

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