Why do I get a wrong answer when I use FIND function in this case?

1 回表示 (過去 30 日間)
Waseem AL Aqqad
Waseem AL Aqqad 2021 年 6 月 17 日
コメント済み: Waseem AL Aqqad 2021 年 6 月 17 日
I have a graph structure "G_dmg.Nodes.Load" which has typically 40 "-inf" values and 10 other numbers ~= -inf.
In my script:
inActive = find(G_dmg.Nodes.Load==-inf);
for ii = 1:length(inActive)
b = neighbors(G_dmg,inActive(ii));
idx = find(G_dmg.Nodes.Load(b)~= -inf);
Load = G_dmg.Nodes.Load(idx);
end
So, I'm trying to find the indices of inActive nodes' neighbors that do not have -inf Load values. FIND function is not giving me back the correct indices.
say b = [10 11 2 3 4] and the 10th, 11th entries in G_dmg.Nodes.Load ~=-inf but 2nd, 3rd, 4th have -inf values. idx variable in my script is giving me back 1 2 which are basically are the first and second entries in b.

採用された回答

Rik
Rik 2021 年 6 月 17 日
When you subindex a variable before using find, it will return the indices relative to that subindexed variable. This is because find doesn't know about the indexing.
This might fix the problem:
idx=b(idx);
  1 件のコメント
Waseem AL Aqqad
Waseem AL Aqqad 2021 年 6 月 17 日
WOW! It works.
Thank you so much for your explanation.

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2021 年 6 月 17 日
編集済み: KSSV 2021 年 6 月 17 日
Replace the line:
inActive = find(G_dmg.Nodes.Load==-inf);
with
idx = isinf(G_dmg.Nodes.Load) ;
inActive = find(sign(G_dmg.Nodes.Load).*idx == -1)
Follow the same for other lines where you are trying to find -inf.
You can achieve the above without loop.
  1 件のコメント
Waseem AL Aqqad
Waseem AL Aqqad 2021 年 6 月 17 日
Thanks for telling me this technique.
Unfortunately, I have to use a loop here since the built in function "neighbors" accepts only scalar inputs, I can't pass the vector inActive to it.
I believe it has this ristriction as each node in a graph has different number of neighboring nodes. Of course, I can concatenate them like this:
b = [];
b = [b, neighbors(G_dmg,inActive(ii))];
But still, I cannot tell which nodes are neighbors to node X and which are neighbors to node Y.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDirected Graphs についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by