Why does my for-loop nested in an if-statement not work?

조회 수: 3 (최근 30일)
F S
F S 2016년 7월 20일
댓글: F S 2016년 7월 20일
I have an for-loop inside an if-statement nested again in another for-loop (I know sounds complicated, example below), which seems to run perfeclty, as long as until the loop is called the SECOND time. Then it gives me the error:
Index exceeds matrix dimensions.
Error in code (line 50)
imb=squeeze(B_proc(n,:,:));
Does anybody have an idea why?
Here the simplified for-if-for-loop:
k=1;
for i=2:length(proc_core)
proc=cell2mat(proc_core(i,2));
sz=size(proc);
l=sz(1);
vs=cell2mat(proc_core(i,3));
if sz(2)==512 && sz(3)==512 && vs==2.5
kl=k+l-1;
Core(k:kl,:,:)=proc;
k=k+l;
elseif vs==2.5
for n=1:length(proc)
imb=squeeze(proc(n,:,:));
imb=imresize(imb,[512 512]);
Core(k,:,:)=imb;
k=k+1;
end
end
end

채택된 답변

Walter Roberson
Walter Roberson 2016년 7월 20일
length(proc) is defined as:
if isempty(proc)
length = 0
else
length = max(size(proc))
end
You, however, used length(proc) as if it were defined as size(proc,1) . If one of the other dimensions of proc is bigger than the number of rows then that bigger dimension is the length.
  댓글 수: 1
F S
F S 2016년 7월 20일
thanks a lot! I suspected there was some other bug in there but I just couldn't find it.

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

추가 답변 (0개)

카테고리

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