Reputation: 549
Beginner matlab user here. I'm trying to write a function that multiplies a and b and returns the product if a and b is positive and -abs(a*b) if any of them is negative. This is what I've got.
function y = MulAnd(a,b)
%MULAND Summary of this function goes here
% Detailed explanation goes here
if(a<0||b<0)
y = -(abs(a*b));
else
y = a*b;
end
end
Matlab doesn't like it. What am I doing wrong?
Upvotes: 0
Views: 4121
Reputation: 24127
I think your code is fine, and I think @OlegKomarov 's comment contains the answer.
When MATLAB crashes unexpectedly due to a problem with the MATLAB Code Analyzer, it adds the name of the file that caused the problem to the file MLintFailureFiles
. This causes the red indicator to appear in the MATLAB file when you later open it.
Try this:
cd(prefdir)
.MLintFailureFiles
, and remove the name of the file (MulAnd.m
).MLintFailureFiles
.Now try again with MulAnd
.
Upvotes: 1
Reputation: 7423
You could try the following which will work with scalars or vectors
function y = MulAnd(a,b)
%MULAND Summary of this function goes here
% Detailed explanation goes here
y = a.*b;
negative = a<0 | b<0;
y(negative) = -abs(y(negative));
end
Upvotes: 1