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

2 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

回答(1 个)

per isakson
per isakson 2018-6-21
编辑:per isakson 2018-6-21
  • 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."

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by