Issue with && in matrix operation.

Hi, i have a problem with the && operators, which it cannot operate the following code:
i = noTier1 + 1;
while i ~= (numNodes + 1)
x = round((area_length - 1) * (rand(10,1) - 0) / (1 - 0) + 1);
y = round((area_width - 1) * (rand(10,1) - 0) / (1 - 0) + 1);
% measure distance between x,y and base station
A = squareform( pdist([x - baseStation.x, y - baseStation.y]) );
A(A > edges) = 0;
if A > tier1Radius && A < tier2Radius
node(i).x = x;
node(i).y = y;
node(i).energy = 1;
node(i).id = i;
node(i).tier = 2;
node(i).pch = 0;
node(i).sch = 0;
node(i).residual = 1;
node(i).centrality = rand;
node(i).commCost = 0;
i = i + 1;
end
end
The error:
Operands to the || and && operators must be convertible to logical scalar values.
Error in MAPmm (line 94)
if A > tier1Radius && A < tier2Radius
Here the input
A = carry matrix value.
noTier1 = 25;
numNodes = 100;
area_length = 100;
area_width = area_length;
baseStation.x = 50;
baseStation.y = 50;
tier1Radius = 25;
tier2Radius = 50;
How can i solve this problem, pls advice me.
Thanks sir.

댓글 수: 4

You're mixing array and element operations; you need to either use logical addressing for all elements from the logic expression or iterate over the array A element-by-element; can't mix both.
Not at all clear what the i loop is iterating over beginning at 25+1 but the A array will be one-based.
As the error says, you need for the specific line
if A(i)>tier1Radius & A(i)<tier2Radius
and iterate of A, but that needs a different loop variable than the i you've got over the elements of A.
Already try use single &, also got error. Then i try this:
if A < tier2Radius | A > tier1Radius
It run smoothly.
Walter Roberson
Walter Roberson 2018년 10월 2일
You just set some A locations to a value, and then you want to test if they ALL satisfy the condition?
Your squareform pdist is returning a 2d array, not a scalar.
Asyran Abdullah
Asyran Abdullah 2018년 10월 2일
Yes, then how can 2d array perform a logical scalar values?

댓글을 달려면 로그인하십시오.

답변 (2개)

the cyclist
the cyclist 2018년 10월 2일

0 개 추천

I suggest that you carefully read the documentation for these "short-circuit" logical operators. They do not operate on vectors -- only scalars.
I'm not sure what you expect they will do, so I'm not sure what to recommend as a replacement. If you describe what you intend, someone can probably help.

댓글 수: 2

Walter Roberson
Walter Roberson 2018년 10월 2일
It would, for example, help if there was a response to https://www.mathworks.com/matlabcentral/answers/421474-how-to-perform-arithmetic-operation-in-pdist#comment_616298
Ok sir,
1. Actually what i want to do is to distribute random node in 100x100 area.
x = round((area_length - 1) * (rand(15,1) - 0) / (1 - 0) + 1);
y = round((area_width - 1) * (rand(15,1) - 0) / (1 - 0) + 1);
2. Then each of the points/nodes will connect as multihop into each other. Which the last blue nodes connect to nearest purple nodes.
3. Then i want to insert some "weights/cost/metric" to each path in order to calculate the shortest path among others.
path = shortestpath(G,1,14);
4. The process will be done in two-tier of network as follow:
However, the blue nodes cannot connect to each other and it difficult to do a shortest path. I hope someone can give a suggestion and advices.
Thanks

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2018년 10월 2일

0 개 추천

You can change the && to &
However remember that there is an implicit all() when testing in if or while so you would be testing whether the condition held for ALL members of A.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2018년 10월 2일

댓글:

2018년 10월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by