Vectorize a for loop

조회 수: 2 (최근 30일)
Ramon Ticzon
Ramon Ticzon 2019년 3월 19일
댓글: Ramon Ticzon 2019년 3월 19일
Hi. How can I speed up the process of my for loop code.
function [missingCase] = CaseChecking(bimage)
missingCase = '';
for img = 1:66
mapList = xlsread('map.xlsx',img);
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

답변 (2개)

Andrei Bobrov
Andrei Bobrov 2019년 3월 19일
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)
ml(:,:,ii) = xlsread('map.xlsx',ii);
end
mlw = permute(ml(:,end:-1:1,:),[1,3,2]);
out = find(~bimage(sub2ind(size(bimage),mlw(:,:,1),mlw(:,:,2))) < 50,'first',1);
  댓글 수: 1
Ramon Ticzon
Ramon Ticzon 2019년 3월 19일
These are the sample files. Thank you

댓글을 달려면 로그인하십시오.


Andrei Bobrov
Andrei Bobrov 2019년 3월 19일
Solution for attached data:
[~,sheets] = xlsfinfo('MapList.xlsx');
n = numel(sheets);
ml = cell(n,1);
for ii = 1:n
ml{ii} = xlsread('MapList.xlsx',ii);
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
Ramon Ticzon
Ramon Ticzon 2019년 3월 19일
where is the variable 'missingCase'? also the 'img' variable.
Ramon Ticzon
Ramon Ticzon 2019년 3월 19일
Hi. Im having an error.
Error in CaseChecking (line 14)
@(x) sum(~bimage(sub2ind(size(bimage),A(x,2),A(x,1)))));

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by