Help me to solve this

5 views (last 30 days)
Ad
Ad on 13 May 2017
Commented: Ad on 15 May 2017
I am very new to matlab and working on image processing in order to detect the object.
I have an Index matrix I: 10 11 12 13 14 15 16 20 21 22 23 24 26 29 30 31 32 37 39 40 Highestvalue, H=13
M=[]; % THIS IS NOT THE FULL CODE
for i=1
for j=1:ILength
if((Distance(H,I(j))<=10)&& (adjMatrix(H,I(j))~=0))
M=[M,j];
end
end
end
I got the result like this: "Result" or M for H 13= 12,13,14.
Expected result is "12 13 14 15 16 20 21 22 23 24 26 29 30 31 32"
Question: How to perform it again for each and every value in the "Result" with no repetitions? For example, again I need to calculate adjacency and distance for 14,12. (For 14, the expected value is 15,16,20) and then for 16, 20 and so on.
Thanks in advance
  3 Comments
Star Strider
Star Strider on 13 May 2017
‘% THIS IS NOT THE FULL CODE’
... that appears to be missing an assignment for ‘HS’ and ‘Result’.
‘H’ is doing as it should, and returning a 3-element vector for row ‘H’:
RowH = [Distance(H,:); adjMatrix(H,:)];
RowHi = RowH(1,:)<=10 & RowH(2,:)~=0;
RowHi =
1×48 logical array
0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Your code works as it should. There is no problem, unless you did not write your code correctly.
We have no idea of knowing what you want to do unless you tell us.
MrsBellamy
MrsBellamy on 13 May 2017
Edited: MrsBellamy on 13 May 2017
The code that you posted will not "return" [12,13,14] but the indices [3,4,5].
It must be M = [M,I(j)];
I imported your data and for H=13, I indeed get the result M=[12,13,14]. For H=14 however, I get M=[13,14,15] and not [15,16,20]. You might wanna check this again?

Sign in to comment.

Accepted Answer

MrsBellamy
MrsBellamy on 13 May 2017
You can solve the problem by introducing the array "usedIndices" which collects all values of H that have been used. "newIndices" are values that are contained in M, but have not been used yet.
M=[];
usedIndices=[];
newIndices=[13];
while ~isempty(newIndices)
for i=length(newIndices)
H=newIndices(i);
for j=1:length(I)
if((Distance(H,I(j))<=10)&& (adjMatrix(H,I(j))~=0))
M=[M,I(j)];
end
end
M = unique(M);
usedIndices = [usedIndices, H];
end
newIndices = setdiff(M,usedIndices);
end
Which indeed yields:
M = [12,13,14,15,16,20,21,22,23,24,26,30,31,32]
  1 Comment
Ad
Ad on 15 May 2017
@MrsBellamy, Thank you so much for the support.
I have one doubt.You have directly assigned highest value to new indices.right?
newIndices=[13];

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!