MATLAB: Speed-up or get rid of loop with find function

for loopget ridgriddingspeed up

Hi guys, maybe anyone have an idea for my problem here. I just struggleing a little bit with finding another way of implementation to get rid of this for-loop. Is there maybe a way to get rid of the slow for-loop? Thank you for your help!
%Create example variables
val = zeros(2000,2000);
a = 1:100:2000;
b = 100:100:2099;
count = 1;
for kk = 1:numel(a)
for jj = 1:numel(a)
for ii = 1:numel(a)
val(a(jj):b(ii),a(kk):b(kk)) = count;
end
count = count + 1;
end
end
list = unique(val);
%Create space between grids (needed speed-up)
for ii = 1:length(list)
[y,x] = find( val == list(ii) );
minx = min(x);
maxx = max(x);
miny = min(y);
maxy = max(y);
minx1 = ceil(minx + 0.05 * (maxx - minx ));
maxx2 = ceil(maxx - 0.02 * (maxx - minx ));
val(y,minx:minx1) = 0;
val(y,maxx2:maxx) = 0;
miny1 = ceil(miny + 0.05 * (maxy - miny ));
maxy2 = ceil(maxy - 0.02 * (maxy - miny ));
val(miny:miny1,x) = 0;
val(maxy2:maxy,x) = 0;
end
What I want to do is, splitting 'val' into seperated grids with space between grids. Maybe there is a better way to do this step 😀

Best Answer

list = randi(100, [100 1]);
val = randi(100, [200 300]);
for ii = 1:length(list)
[y,x] = find( val == list(ii) );
minx = min(x);
maxx = max(x);
minx1 = ceil(minx + 0.05 * (maxx - minx ));
maxx2 = ceil(maxx - 0.02 * (maxx - minx ));
val(y,minx:minx1) = 0;
val(y,maxx2:maxx) = 0;
end
image(val)
Is this really what you call "splitting 'val' into separated grids"?
Please explain what you exactly want to achieve. I guess there is a better method than this loop.
Related Question