Info

This question is closed. Reopen it to edit or answer.

How to subtract from a vector, have it return a value of zero if the answer is less than zero, and then tell me how much was subtracted?

1 view (last 30 days)
I essentially need to write a function like this:
x=[1, 2 3]
y= 2
x(1) - y
if x(1)<0
totalsubtracted = y +x(1)
x(1)=0
else
totalsubtracted = x(1)-y
end
But without conditionals. How do I do this?

Answers (1)

madhan ravi
madhan ravi on 15 Sep 2020
totalsubtracted = (x(1) < 0) * (y + x(1)) + (x(1) > 0) * (- y + x(1)) % ?
  8 Comments

This question is closed.

Tags

Community Treasure Hunt

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

Start Hunting!