Consider Preallocating for speed

조회 수: 7 (최근 30일)
Loïc Majerus
Loïc Majerus 2016년 5월 12일
댓글: Walter Roberson 2016년 5월 12일
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 ?

답변 (1개)

Walter Roberson
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
Loïc Majerus
Loïc Majerus 2016년 5월 12일
Datastruct = uiimport ('standard.xls'); % cell array
pause(20)
standard = datastruct.standard;
for i=1:length(standard)
for j=1:13
if ischar(standard{i,j})==1
standard{i,j}= str2double (standard{i,j});
end
end
end
B=cell2mat(standard);
Thanks for the answer but now, I can't get my folder in cell array.. Matlab is saying that : Undefined variable "datastruct" or function "datastruct.standard".
Error in Importation (line 3) standard = datastruct.standard;
Moreover can you tell me how too get my scripts with all the line and the color (as it is in Matlab) for putting them in a word document ?
Walter Roberson
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 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