I get this error please help: 'Operands to the || and && operators must be convertible to logical scalar values"
Show older comments
I am writing a function that takes two binary numbers and adds them up. The output is the sum of the numbers in binary and decimal. I submitted this through Cody and it passes all the tests but for some reason I cannot make it run.
Please Help
function [DSum,BSum] = BinaryAdd2(B1,B2)
%error check
if (B1 ~= 1 || B1 ~=0)
error('the binary number entered must contain 1s and 0s only');
end
if (B2 ~= 0 || B2 ~=1)
error('the binary number entered must contain 1s and 0s only');
end
if (B1 == -1 || B2 == -1)
error('the binary number entered must contain positive integers');
end
% converts from binary to decimal
sum1=0;
sum2=0;
for i=1:numel(B1)
if B1(i)==1
DSum1=i^2;
end
sum1=DSum1+sum1;
end
for i=1:numel(B2)
if B1(i)==1;
DSum2=i^2;
end
sum2=DSum2+sum2;
end
DSum=sum1+sum2;
% now the sum is in decimal form so it has to be converted back to binary
i = 1; %first iteration outside the loop
quotient = floor(DSum/2);
reminder = rem(DSum, 2);
BinNum(i) = num2str(reminder(i)); %convet 1st binary number to string
while 2 <= quotient % while the quotient is less greater or equals than 2
DSum = quotient;
i = i + 1; % iterations
quotient = floor(DSum/2); %rounds the division towards minus infinity
reminder = rem(DSum, 2); %reminder of division
BinNum(i) = num2str(reminder);
end
BinNum(i+1) = num2str(quotient); % converts the rest of bin # to string
BSum = fliplr(BinNum); %reverses the order of numbers = the actual binary conversion
end
Answers (1)
Geoff Hayes
on 10 Dec 2014
Ruben - your inputs to the BinaryAdd2 function, B1 and B2, must be strings representing binary numbers. So what you are trying to do is to ensure that only 0's and 1's are in the string, however, if you look at something like
B1 ~= 1
you are comparing each character in B1 against the number 1, and so a vector (whose length is the number of characters in the binary string) of all ones is returned. As is, this vector cannot be converted to a logical scalar value (true or false) and so the error is generated. And this isn't what you want to be doing anyway - you cannot compare characters with numbers, you must compare characters with characters. Even if we replace the above with
B1 ~= '1'
this still doesn't produce the correct answer.
Consider using regexp to check to see if the string contains anything BUT 0 and 1. Consider the following
regexp(B1,'[^01]','match')
which will return either a cell array of the characters that are neither 0 or 1. We use the ^ in our pattern to indicate that we are interested in any character but those contained in the square brackets (so anything except zero or one). If there are no matches, then an empty cell is returned, and we can use this to our advantage. Try changing your code to
if ~isempty(regexp(B1,'[^01]','match'))
error('the binary number B1 entered must contain 1s and 0s only');
elseif ~isempty(regexp(B2,'[^01]','match'))
error('the binary number B2 entered must contain 1s and 0s only');
end
There is no need for the third if statement as the above is sufficient to capture anything except 0's and 1's.
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!