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
Amit 2014년 1월 22일

0 개 추천

A simple test is type
[Dim{128}(1) Dim{128}(2)+D]
and
Dim{144}
and see if they match.

댓글 수: 17

Ayob
Ayob 2014년 1월 22일
편집: Ayob 2014년 1월 22일
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
Amit 2014년 1월 22일
what is if orders ?
Ayob
Ayob 2014년 1월 22일
편집: Ayob 2014년 1월 22일
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
try
if isequal(Dim{j},[Dim{i}(1) Dim{i}(2)+D])
and see if it makes a difference.
Ayob
Ayob 2014년 1월 22일
OK, I will check it, but maybe this is a MATlAB weakness point? And it should be checked.
Amit
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.
Ayob
Ayob 2014년 1월 22일
편집: Ayob 2014년 1월 22일
*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
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
Ayob 2014년 1월 22일
Yes, I will upload my code.m.
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.
Ayob
Ayob 2014년 1월 22일
편집: Ayob 2014년 1월 22일
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
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.
Ayob
Ayob 2014년 1월 22일
편집: Ayob 2014년 1월 22일
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
Amit 2014년 1월 22일
I was looking at the figure you posted in your original post.
Amit
Amit 2014년 1월 22일
편집: Amit 2014년 1월 22일
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
Ayob 2014년 1월 22일
It's probably right.I will check it.
Ayob
Ayob 2014년 1월 22일
IT WORKS., THANKS GOD., THANKS Amit.

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

추가 답변 (0개)

카테고리

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

질문:

2014년 1월 22일

댓글:

2014년 1월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by