Problem with if statments

12 views (last 30 days)
Quentin
Quentin on 14 Mar 2013
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

Answers (1)

Matt Kindig
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
Quentin
Quentin on 14 Mar 2013
Hi Matt, Thanks for getting back to me.
I see what you mean, but the only issue is that i each element of the Pwr_rec_from_BS# is a 'user'.
I need to compare each 'power recieved' with the same element of all of the other 'power recieved's.
For example, 'user 3' is the third element of 'Pwr_rec_from_#'. This user's power reveieved from BS1 must be compared with BS2, BS3, BS4 etc.
So essentially, each 'user' has 7 values associated with him.
The output I need is a matrix/vector with 1000 elements in a row, each assigned with a number 1-7 depending on which base station gives the most power.
Thanks again for getting back to me.
Quentin
Matt Kindig
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!