# Vectorize a for loop

1 view (last 30 days)
Ramon Ticzon on 19 Mar 2019
Commented: Ramon Ticzon on 19 Mar 2019
Hi. How can I speed up the process of my for loop code.
function [missingCase] = CaseChecking(bimage)
missingCase = '';
for img = 1:66
counter=0;
for i = 1:200
if (~bimage(mapList(i,2),mapList(i,1)))
counter=counter+1;
end
end
if counter<50
missingCase = img;
break
end
end

Andrei Bobrov on 19 Mar 2019
Please attach your data (bimage,map.xlsx - small example) as mat-file and xlsx-file.
ml = zeros(200,2,66);
for ii = 1:size(ml,3)
end
mlw = permute(ml(:,end:-1:1,:),[1,3,2]);
out = find(~bimage(sub2ind(size(bimage),mlw(:,:,1),mlw(:,:,2))) < 50,'first',1);
Ramon Ticzon on 19 Mar 2019
These are the sample files. Thank you

Andrei Bobrov on 19 Mar 2019
Solution for attached data:
[~,sheets] = xlsfinfo('MapList.xlsx');
n = numel(sheets);
ml = cell(n,1);
for ii = 1:n
end
jj = repelem((1:n)',cellfun('size',ml,1));
A = cat(1,ml{:});
k = accumarray(jj,(1:size(A,1))',[],...
@(x) sum(~bimage(sub2ind(size(bimage),A(x,2),A(x,1)))));
out = find(k < 50,'first',1);
##### 2 CommentsShowHide 1 older comment
Ramon Ticzon on 19 Mar 2019
Hi. Im having an error.
Error in CaseChecking (line 14)
@(x) sum(~bimage(sub2ind(size(bimage),A(x,2),A(x,1)))));