MYSTERY matlab Error - Index exceeds matrix dimensions - can't track it!

1 view (last 30 days)
hi everyone , i have this silly error but i'm not able to fix it , since i don't see any index exceeding the matrix dimensions .
function [ Hclean,Bclean , EnergyDissipation ,InnerMagnetization ] = BHavg( B,H )
for t=1:size(H,1)
InnerB=[];
InnerBnegative=[];
ZeroIndexTemp = find(abs(H(t,:))-0.03<0.005);
for p =1:size(ZeroIndexTemp,2)
if B(t,ZeroIndexTemp(p))>0
InnerB=[InnerB,B(t,ZeroIndexTemp(p))];
else
InnerBnegative=[InnerBnegative ,B(t,ZeroIndexTemp(p))];
end
end
InnerMagnetization(t) = mean(InnerB);
ErrorManetization(t) = abs(InnerMagnetization(t) + mean(InnerBnegative));
ZeroIndex =[];
for i =1:size(ZeroIndexTemp,2)-1
if ZeroIndexTemp(i+1)-ZeroIndexTemp(i)>10
ZeroIndex=[ZeroIndex,ZeroIndexTemp(i)];
end
end
loopHTemp={};
loopH=[];
loopB=[];
loopBTemp={};
for j = 1:(size(ZeroIndex,2)-2)/2
loopHTemp{j}=H(ZeroIndex(j*2):ZeroIndex(j*2+2));
loopBTemp{j}=B(ZeroIndex(j*2):ZeroIndex(j*2+2));
end
VsizeCount =[];
for k=1:size(loopHTemp,2)
VsizeCount=[VsizeCount,size(loopHTemp{k},2)];
end
minListSize = min(VsizeCount);
for m=1:size(loopHTemp,2)
loopHTemp{m}(minListSize+1:end)=[];
loopH(m,:)=loopHTemp{m};
loopBTemp{m}(minListSize+1:end)=[];
loopB(m,:)=loopBTemp{m};
end
A=[];
B=[];
for y=1:size(loopH,1)
A=[A;loopH(y,:)];
B=[B;loopB(y,:)];
end
if size(loopH,1)>1
Hclean(t,:) = mean(A);
Bclean(t,:) = mean(B);
else
Hclean(t,:) = loopH(:,i);
Bclean(t,:) = loopB(:,i);
end
EnergyDissipation(t) = polyarea(Hclean(t,:),Bclean(t,:));
if EnergyDissipation(t) >5
figure
fill(Hclean(t,:),Bclean(t,:),'b')
end
end
end
i added the entire code since maybe it's not related to the error message matlab shows me , but this is what it shows
Index exceeds matrix dimensions.
Error in BHavg (line 8)
if B(t,ZeroIndexTemp(p))>0
please help ! (:
  6 Comments
per isakson
per isakson on 21 Jun 2018
"i guessed correctly" You shouldn't guess, but consult the documentation!

Sign in to comment.

Answers (1)

per isakson
per isakson on 21 Jun 2018
Edited: per isakson on 21 Jun 2018
  • B is overwritten at line 43 and the following loop.
  • In the second and the following iterations of the outer loop, t>=2, the size of B may be so small that B(t,ZeroIndexTemp(p)) causes the error. "Index exceeds matrix dimensions."

Community Treasure Hunt

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

Start Hunting!