Info
此问题已关闭。 请重新打开它进行编辑或回答。
How to subtract from a vector array and, if the answer is lees than 0, have it return a value of 0
2 次查看(过去 30 天)
显示 更早的评论
I'm trying to subtract a number from a specific value in a vector. Then, if the answer is less than 0, I want it to return the answer with it being rounded to 0.
this is what I meam
%example%
x = [1 2 3]
x(1)-2
%the result would look like this%
x =
-1 2 3
%but I want it to look like this%
x =
0 2 3
How do I do this without using conditionals?
0 个评论
回答(2 个)
David Hill
2020-9-15
x=[1 2 3];
x=x-2;
x(x<0)=0;
2 个评论
David Hill
2020-9-15
x=[1 2 3];
amountbelowzero=zeros(size(x));
x=x-2;
amountbelowzero(x<0)=abs(x(x<0));
x(x<0)=0;
John D'Errico
2020-9-15
编辑:John D'Errico
2020-9-15
Simple. No need to test anything either. Just use the max function.
x = [1 2 3];
x(1) = max(x(1) - 2,0);
x
x =
0 2 3
0 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!