why? the number of elements in B and I must be the same
Show older comments
I am runnning the following code to get AIC CAIC and SBC ,I use loop
for i=1:10
if i==1
...
if i==10
[PHAT,PCI]=mle(data,'dist','t');
[nlogL,AVAR]=tlike(PHAT,data);
figure(10)
h2=histfit(data,10,'t');hold on;
set(h10(1),'facecolor','w');
set(h10(2),'color','r');
end
k=size(PHAT,2);
n=size(data);
AIC=2*nlogL+2*k;
CAIC=2*nlogL+k*(log(n)+1);
SBC=2*nlogL+k*log(n);
eigenval=eig(AVAR);
ave=mean(eigenval);
CIF_IFIM=(1/(4*ave^2))*sum((eigenval-ave).^2);
ICOMP=2*nlogL+2*CIF_IFIM;
X_AIC(i)=AIC;
X_CAIC(i)=CAIC;
X_SBC(i)=SBC;
X_ICOMP(i)=ICOMP;
AIC=NaN;
CAIC=NaN;
SBC=NaN;
ICOMP=NaN;
PHAT=NaN;
AVAR=NaN;
nLOGL=NaN;
end
for these part :
AIC=2*nlogL+2*k;
CAIC=2*nlogL+k*(log(n)+1);
SBC=2*nlogL+k*log(n);
I got the message from matlab saying : The variable "X_ICOMP" appears to change size on every loop iteration (within a script). consider preallocating for speed . also the word in red appears in the command window: "In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in q2 (line 96)
X_CAIC(i)=CAIC;"
I want to know why will this happens ?How to deal with this issue ? Thanks !
Answers (2)
dpb
on 9 Feb 2015
The preallocation is simply before the loop add
X_ICOMP=zeros(10,1); % preallocate for later population
and similar for the other arrays. This way Matlab "knows" the size a priori and doesn't have to reallocate on each pass through the loop. For very small loops like this you'll not see much speed increase but if N were in the thousands, say, it can become a real performance bottleneck.
As for the assignment size mismatch,
X_CAIC(i)=CAIC;
the term X_CAIC(i) is a single location within the array and can, therefore, only have a single value for the assignment. "WHY" is because up early on you wrote
n=size(data);
...
CAIC=2*nlogL+k*(log(n)+1);
n is a vector of at least 2 even if as one would presume, data is a 1D array, size returns the '1' for the second dimension. Hence, CAIC is also vector of length two as log(n) will return a value for each element in n. I would guess you intend
n=length(data);
instead of size
Stephen23
on 9 Feb 2015
0 votes
You are asking about several different issues here, so lets first have a look at preallocation:
Basically the idea of preallocation is to let MATLAB know how big your data matrices are going to be before any loops, which makes its memory management much much faster. For more information, read the following links. If you use your favorite search engine with the terms "MATLAB preallocation" you will find plenty of results to help you on the topic. For a start, have a look at what MATLAB themselves have to say about this. Then you might like to search this forum, which also has many answers on the same topic, some of them also answered by MATLAB .
The second issue is "number of elements must be the same". It seems that you are likely trying to put a vector of values into a single element of an array. You should have a look at the size of CAIC: use the debugger, or place disp(numel(CAIC)) on the line before line 96. Then run your code again. Is CAIC scalar (1x1), or does it contain more elements?
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!