Why doesn't this simple search program work?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I wrote a program to give me some nodes as shown in the figure

For each node like i, I used 'Dim{i}=[something something]' in my programming which introduces the position of each node.
Each node has contact which some other nodes like what is shown for i=128

.
For each node like i, I introduce position as given below (from 1 to 13) :

.
For example:
NODZ{i}(1)=127
NODZ{i}(2)=144
...
NODZ{i}(13)=128
Differences between nodes is a constant value and is called D in my programming.
In part of my program, I used a kind of search program to give me neighborhood of each node like i for all p nodes:
for i=1:p
NODZ{i}=zeros(1,13);
for j=1:p
if Dim{j}==[Dim{i}(1)-D Dim{i}(2)]
NODZ{i}(1)=j;
end
if Dim{j}==[Dim{i}(1) Dim{i}(2)+D]
NODZ{i}(2)=j;
end
if Dim{j}==[Dim{i}(1)+D Dim{i}(2)]
NODZ{i}(3)=j;
end
if Dim{j}==[Dim{i}(1) Dim{i}(2)-D]
NODZ{i}(4)=j;
end
if Dim{j}==[Dim{i}(1)-D Dim{i}(2)+D]
NODZ{i}(5)=j;
end
if Dim{j}==[Dim{i}(1)+D Dim{i}(2)+D]
NODZ{i}(6)=j;
end
if Dim{j}==[Dim{i}(1)+D Dim{i}(2)-D]
NODZ{i}(7)=j;
end
if Dim{j}==[Dim{i}(1)-D Dim{i}(2)-D]
NODZ{i}(8)=j;
end
if Dim{j}==[Dim{i}(1)-2*D Dim{i}(2)]
NODZ{i}(9)=j;
end
if Dim{j}==[Dim{i}(1) Dim{i}(2)+2*D]
NODZ{i}(10)=j;
end
if Dim{j}==[Dim{i}(1)+2*D Dim{i}(2)]
NODZ{i}(11)=j;
end
if Dim{j}==[Dim{i}(1) Dim{i}(2)-2*D]
NODZ{i}(12)=j;
end
if Dim{j}==[Dim{i}(1) Dim{i}(2)]
NODZ{i}(13)=j;
end
end
end
BUT, when I run the program and ask it something like NODZ{128} it gives me:
>> NODZ{128}
ans =
127 0 129 113 0 0 114 112 126 160 130 0 128
This not correct because real value of NODZ{128} should be :
NODZ{128}=[127 160 129 113 143 145 114 112 126 160 130 99 128]
Why some members becomes zero, How can I solve it?
採用された回答
Amit
2014 年 1 月 22 日
A simple test is type
[Dim{128}(1) Dim{128}(2)+D]
and
Dim{144}
and see if they match.
17 件のコメント
Actually my problem is that they match in the value but "IF orders" I've used sometimes doesn't work . I'm confused about it an can not find the problem in this simple codes.
for example:
Dim{160}=[Dim{128}(1) Dim{128}(2)+D] in Matlab check but for i=128 and j=160 in the if order
if Dim{j}==[Dim{i}(1) Dim{i}(2)+D]
NODZ{i}(2)=j;
end
if order doesn't work!!?
Amit
2014 年 1 月 22 日
what is if orders ?
CODE:
for i=1:p
NODZ{i}=zeros(1,13);
for j=1:p
if Dim{j}==[Dim{i}(1)-D Dim{i}(2)] * _%These are if orders_ *
NODZ{i}(1)=j;
end
if Dim{j}==[Dim{i}(1) Dim{i}(2)+D]
NODZ{i}(2)=j;
end
if Dim{j}==[Dim{i}(1)+D Dim{i}(2)]
NODZ{i}(3)=j;
end
if Dim{j}==[Dim{i}(1) Dim{i}(2)-D]
NODZ{i}(4)=j;
end
if Dim{j}==[Dim{i}(1)-D Dim{i}(2)+D]
NODZ{i}(5)=j;
end
if Dim{j}==[Dim{i}(1)+D Dim{i}(2)+D]
NODZ{i}(6)=j;
end
if Dim{j}==[Dim{i}(1)+D Dim{i}(2)-D]
NODZ{i}(7)=j;
end
if Dim{j}==[Dim{i}(1)-D Dim{i}(2)-D]
NODZ{i}(8)=j;
end
if Dim{j}==[Dim{i}(1)-2*D Dim{i}(2)]
NODZ{i}(9)=j;
end
if Dim{j}==[Dim{i}(1) Dim{i}(2)+2*D]
NODZ{i}(10)=j;
end
if Dim{j}==[Dim{i}(1)+2*D Dim{i}(2)]
NODZ{i}(11)=j;
end
if Dim{j}==[Dim{i}(1) Dim{i}(2)-2*D]
NODZ{i}(12)=j;
end
if Dim{j}==[Dim{i}(1) Dim{i}(2)]
NODZ{i}(13)=j;
end
end
end
Amit
2014 年 1 月 22 日
try
if isequal(Dim{j},[Dim{i}(1) Dim{i}(2)+D])
and see if it makes a difference.
Ayob
2014 年 1 月 22 日
OK, I will check it, but maybe this is a MATlAB weakness point? And it should be checked.
Amit
2014 年 1 月 22 日
The thing is, what you're trying to do can be done more simply without going through so many loop.
Like for example, if you store your original node information in a matrix, you don need a cell to store that information.
*It still doesn't work!* Is there any other way to set NODZ{i} matrices, I'm disappointed of Matlab in this type of code writing.
Amit
2014 年 1 月 22 日
Don't be dissapointed with MATLAB. There must be something happening. Can you save the workspace with value for Dim and upload? If I have Dim values, I might be able to help you better.
Ayob
2014 年 1 月 22 日
Yes, I will upload my code.m.
Amit
2014 年 1 月 22 日
not your code. I need Dim values.
Once you've generated Dim cell, do this:
save('ToUpload.mat',Dim)
This will create ToUpload.mat which you should upload.
I've uploaded it.I'm sure about 'Dim' because it gives me true virtual plot shown in untitled.fig . I think all the problem is with IF orders.
Amit
2014 年 1 月 22 日
From your file: Dim{144} = [-0.0400 -0.0640]; Dim{128} = [-0.0360 -0.0680];
From you if statement: Dim{144} == [Dim{128}(1) Dim{128}(2)+D]
But Dim{144}(1) does not matches with Dim{128}(1), Then how can you expect MATLAB to say that they are equal.
No, you made a mistake. As you see in 'untitled.fig' i=144 is in the left-top of i=128 so its position is 5 so this If order should be activated for it:
if Dim{j}==[Dim{i}(1)-D Dim{i}(2)+D]
NODZ{i}(5)=j;
end
Dim{144}==[Dim{128}(1)-D Dim{128}(2)+D]
(In this example D=0.04)

.

Amit
2014 年 1 月 22 日
I was looking at the figure you posted in your original post.
Here is the thing. Sometimes in floating point numbers, you get very small decimal deference. In which case they both will not be equal. for example 0.01 and 0.01000000001.
you should try something like this
tol = 1e-6; % Define Tolerance
for ... % Rest of the code
for ..
if norm(Dim{j}-[Dim{i}(1)-D Dim{i}(2)+D]) < tol
This takes care of small differences in floating point numbers.
Ayob
2014 年 1 月 22 日
It's probably right.I will check it.
Ayob
2014 年 1 月 22 日
IT WORKS., THANKS GOD., THANKS Amit.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
