How can I find a connected path on a matrix by Random Search?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
bkshn
el 26 de Abr. de 2014
Comentada: bkshn
el 29 de Mayo de 2014
Hello
I have a matrix . I want to find a connected path of elements from up to button. I want to do it by random search. As I said I want connected path ,then if it selects element [i,j] in row i , in the next row (i+1) it should select from column j-1 or j or j+1. I want to do it by random search.
I will appreciate your help.
0 comentarios
Respuesta aceptada
rifat
el 27 de Abr. de 2014
Editada: rifat
el 27 de Abr. de 2014
Hope the following function helps.
function[]= randSelection(mat)
[r c]=size(mat);
list=1:c;
for row=1:r
col=list(1 + floor(rand() * length(list)));
l1=col-1;
l2=col+1;
if l1<1
l1=1;
end
if l2>c
l2=c;
end
list=l1:l2;
mat(row,col)
end
5 comentarios
Más respuestas (1)
Image Analyst
el 27 de Mayo de 2014
Why random? Why not use a function built for it that does it in a systematic way? If you have the Image Processing Toolbox, see http://blogs.mathworks.com/steve/2011/11/01/exploring-shortest-paths-part-1/ Or you could simply use bwlabel() to get a list of all elements on the path.
6 comentarios
Image Analyst
el 27 de Mayo de 2014
Here I make a matrix that has a roughly "S" shaped path. Then I run rifat's code on it:
function test
clc;
mat = [...
0 0 0 1 1 1 0
0 0 1 0 0 0 0
0 1 0 0 0 0 0
0 0 1 1 1 0 0
1 0 0 0 1 0 0
0 1 1 0 0 1 0
0 0 0 1 1 1 0
0 0 0 0 0 0 0]
randSelection(mat)
function[]= randSelection(mat)
[r c]=size(mat);
list=1:c;
for row=1:r
col=list(1 + floor(rand() * length(list)));
l1=col-1;
l2=col+1;
if l1<1
l1=1;
end
if l2>c
l2=c;
end
list=l1:l2;
mat(row,col)
end
Save all of the above code (both functions) in test.m and run it and you'll get this:
mat =
0 0 0 1 1 1 0
0 0 1 0 0 0 0
0 1 0 0 0 0 0
0 0 1 1 1 0 0
1 0 0 0 1 0 0
0 1 1 0 0 1 0
0 0 0 1 1 1 0
0 0 0 0 0 0 0
ans =
0
ans =
0
ans =
0
ans =
1
ans =
1
ans =
0
ans =
1
ans =
0
Now if that's what you want, fine, but I'm just not getting it. I don't see anything that describes the location of the pixels (elements) on the "S" shaped path. I would think the path would be a list of 16 (row, column) coordinates, not a list of 8 zeros or ones (not even really a list since they're not in the same array). But I'm glad it's working for you. I don't need to understand it - you do and that's all that counts.
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!