Consider Preallocating for speed
조회 수: 7 (최근 30일)
이전 댓글 표시
uiimport ('standard.xls'); %cell array
pause(20)
for i=1:length(standard)
for j=1:13
if ischar(standard{i,j})==1 % si l'élement {i,j}
standard{i,j}= str2double (standard{i,j}); %line 6
end
end
end
B=cell2mat(standard);
Matlab says this for line 6:
The size of the indicated variable or array appears to be changing with each loop iteration. Commonly, this message appears because an array is growing by assignment or concatenation. Growing an array by assignment or concatenation can be expensive. For large arrays, MATLAB must allocate a new block of memory and copy the older array contents to the new array as it makes each assignment.
Programs that change the size of a variable in this way can spend most of their run time in this inefficient activity. There is also significant overhead in shrinking an array on each iteration or in changing the size of a variable on each iteration. In such cases, MATLAB must reallocate and copy the contents repeatedly.
Someone can help me ?
댓글 수: 0
답변 (1개)
Walter Roberson
2016년 5월 12일
The warning is not relevant that this particular code. MATLAB is not noticing that you are "poofing" the variable named standard into existence will its full size and thinks that you are expanding the array as you go.
You can get around the warning (and improve your code) by using
datastruct = uiimport ('standard.xls'); %cell array
standard = datastruct.standard;
댓글 수: 2
Walter Roberson
2016년 5월 12일
You saved the data into Datastruct with a capital D when it should have been datastruct with a lowercase D.
You can "publish" in Word format; see http://www.mathworks.com/help/matlab/ref/publish.html#btbso84 . I have never tried that.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!