Consider Preallocating for speed

4 Ansichten (letzte 30 Tage)
Loïc Majerus
Loïc Majerus am 12 Mai 2016
Kommentiert: Walter Roberson am 12 Mai 2016
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 ?

Antworten (1)

Walter Roberson
Walter Roberson am 12 Mai 2016
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 Kommentare
Loïc Majerus
Loïc Majerus am 12 Mai 2016
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 am 12 Mai 2016
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by