Consider Preallocating for speed

4 次查看(过去 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 ?

回答(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