Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

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 (최근 30일)
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?

답변 (1개)

madhan ravi
madhan ravi 2020년 9월 15일
totalsubtracted = (x(1) < 0) * (y + x(1)) + (x(1) > 0) * (- y + x(1)) % ?
  댓글 수: 8
madhan ravi
madhan ravi 2020년 9월 15일
Ok ,so wait for the response. Don’t ask the same question a billion times before responding here.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by