not sure how to approach this

3 次查看(过去 30 天)
I have a 2D matrix. Let's say A=80x100; I need a way to visit each cell in this matix only once. Moreover my problem is a little more involved than that. Not only do I want to visit each cell only once but the user also defines a window of certain size. Let's say WINDOW=3x3. Then let's say I randomly choose a location on A that corresponds to indecies [2,3]. Then this is considered a central location and in my algorithm I will also consider points around this central location as specified by the window. Hence in this example I will also consider points at indecies [1,3],[3,3],[1,2],[2,2],[3,2],[1,4],[2,4],[3,4]. So in the next round when I am trying to figure out which cell in A to visit, I will not consider these 9 points in my random draw. Any suggestions? Thanks!

采纳的回答

Matt J
Matt J 2013-2-1
编辑:Matt J 2013-2-1
A=rand(80,100);
[m,n]=size(A);
winsize=3;
midwin=winsize/2+.5;
[dx,dy]=ndgrid(1:winsize);
jumps = sub2ind([m,n],dx,dy)-sub2ind([m,n],midwin,midwin);
B=A;
B(:)=1:numel(A);
B=B(midwin:end+1-midwin, midwin:end+1-midwin);
points=B(:);
numpoints=length(points);
while numpoints
randlocation=points(randi(numpoints));
window=randlocation+jumps;
%%%%%%%%%%%%do stuff to A(window)
points=setdiff(points,window(:));
numpoints=numel(points);
end

更多回答(2 个)

Sean de Wolski
Sean de Wolski 2013-2-1
idx = randperm(numel(A));
To give you a list of random linear indices to visit.
Then use a for-loop to loop over these. When you get to one index, use ind2sub() to convert it to row/column coordinates (or do this before hand for all of them and loop over that.)
doc ind2sub %for more info
Then make your window by adding and subtracting floor(window_size/2) to the indices and do whatever operation you have on this. You could also do this addition/subtraction before hand and loop over that instead.

jenka
jenka 2013-2-1
This is sooo great! I am so thankful to you!
  3 个评论
jenka
jenka 2013-2-1
Hi Matt,no, I did not know that. Will do this for sure. I have a quick question regarding border's effect of this problem. I am not sure how to quickly fix this? For example, assume
A=rand(4,5).
Then if randlocation is equal to 16.
The window is
window =
11 15 19
12 16 20
13 17 21
Which is not entirely correct. As in this case the window should be
window =
11 15 19
12 16 20
Any suggestions? Thanks
Matt J
Matt J 2013-2-1
编辑:Matt J 2013-2-1
In the code I gave you, randlocation will never be on the boundary. I excluded all locations without a full winsize x winsize neighborhood in this line
B=B(midwin:end+1-midwin, midwin:end+1-midwin);
If you're saying that you can't ignore boundary points, you could pre-embed your A matrix into a larger matrix, and modify as follows
A=rand(80,100);
winsize=3;
midwin=winsize/2+.5;
A0=A; %save original A
A=nan(size(A0)+winsize-1);
A(midwin:end+1-midwin, midwin:end+1-midwin)=A0; %enlarge
[m,n]=size(A);
[dx,dy]=ndgrid(1:winsize);
jumps = sub2ind([m,n],dx,dy)-sub2ind([m,n],midwin,midwin);
B=A;
B(:)=1:numel(A);
B=B(midwin:end+1-midwin, midwin:end+1-midwin);
points=B(:);
numpoints=length(points);
while numpoints
randlocation=points(randi(numpoints));
window=randlocation+jumps;
window(isnan(A(window)))=[]; %THROW AWAY NANS!!!!!!!!!!!!!
%%%%%%%%%%%%do stuff to A(window)
points=setdiff(points,window(:));
numpoints=numel(points);
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by