Problem with if statments
12 views (last 30 days)
Show older comments
I am aware of the fact that my code is not very glamorous, but i'm still having problems with it!
for some reason I keep getting
"Operands to the and && operators must be convertible to logical scalar values"
my code is...
if (Pwr_rec_from_BS1 > Pwr_rec_from_BS2) && (Pwr_rec_from_BS1 > Pwr_rec_from_BS3) && (Pwr_rec_from_BS1 > Pwr_rec_from_BS4) && (Pwr_rec_from_BS1 > Pwr_rec_from_BS5) && (Pwr_rec_from_BS1 > Pwr_rec_from_BS6) && (Pwr_rec_from_BS1 > Pwr_rec_from_BS7)
BS_assign_PR=1;.
elseif (Pwr_rec_from_BS2 > Pwr_rec_from_BS1 && Pwr_rec_from_BS2 > Pwr_rec_from_BS3 && Pwr_rec_from_BS2 > Pwr_rec_from_BS4 && Pwr_rec_from_BS2 > Pwr_rec_from_BS5 && Pwr_rec_from_BS2 > Pwr_rec_from_BS6 && Pwr_rec_from_BS2 > Pwr_rec_from_BS7)
BS_assign_PR=2;
etc.... all the way down to ...
elseif (Pwr_rec_from_BS7 > Pwr_rec_from_BS1 && Pwr_rec_from_BS7 > Pwr_rec_from_BS2 && Pwr_rec_from_BS7 > Pwr_rec_from_BS3 && Pwr_rec_from_BS7 > Pwr_rec_from_BS4 && Pwr_rec_from_BS7 > Pwr_rec_from_BS5 && Pwr_rec_from_BS7 > Pwr_rec_from_BS6)
BS_assign_PR=7;
end
I have 7 matrices of 1000 'users' in each. I am trying to compare each user's power received from each "base station", and assign each user with a number (from 1-7) depending on which base station they receive the most power from...
I have only just started using MATLAB so am not too sure on where I am going wrong.
Any help would be greatly appreciated.
(PS, if I change && to &, i get an error saying my BS_assign_PR is undefined!)
Cheers
Quentin
0 Comments
Answers (1)
Matt Kindig
on 14 Mar 2013
Edited: Matt Kindig
on 14 Mar 2013
The error is because you are comparing a vector of (presumably) 1000 elements to another vector of 1000 elements. Thus, when you write:
(Pwr_rec_from_BS1 > Pwr_rec_from_BS2)
you are returning a vector of 1000 logical elements (that is, the comparison is done for each element in Pwr_rec_from_BS1 comparing to Pwr_rec_from_BS2). Since this vector does not necessarily contain all of the same true/false value (e.g., the first element of Pwr_rec_from_BS1 might be greater than the first element of Pwr_rec_from_BS2, but the second element of Pwr_rec_from_BS1 may not be greater than the second element of Pwr_rec_from_BS2), the if statement cannot unambiguously determine the truth of your statement.
In order to use the 'if' statement, you need to reduce this result to a scalar (1-element) result. Two ways of doing this:
all(Pwr_rec_from_BS1 > Pwr_rec_from_BS2)
%requires that *every* element in Pwr_rec_from_BS1 is greater than the corresponding element in Pwr_rec_from_BS2)
any(Pwr_rec_from_BS1 > Pwr_rec_from_BS2)
%requires that *at least one* element in Pwr_rec_from_BS1 is greater than the corresponding element in Pwr_rec_from_BS2)
Thus, your if statements would be written as:
if all(Pwr_rec_from_BS1 > Pwr_rec_from_BS2) && all(Pwr_rec_from_BS1 > Pwr_rec_from_BS3) && [rest of conditions here]
2 Comments
Matt Kindig
on 16 Mar 2013
Edited: Matt Kindig
on 16 Mar 2013
Hi Quentin,
Sorry for not getting back to you sooner--I lost track of this thread. Anyway, the best way to do this is not to use 'if' statements at all, but instead to use the second output of the 'max' command. Try this:
%Assuming Pwr_rec_from_BS1, etc. are all 1x1000 matrices
All_Pwr = [ Pwr_rec_from_BS1; Pwr_rec_from_BS2; Pwr_rec_from_BS3; Pwr_rec_from_BS4; Pwr_rec_from_BS5; Pwr_rec_from_BS6; Pwr_rec_from_BS7];
[~, MaxPwr] = max(All_Pwr, [], 1);
%this should give a 1x1000 matrix with the largest base station given for each user
See Also
Categories
Find more on Other Formats 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!