Variable appears to change size on every loop iteration

Hi to everybody i have a problem running these codes .its a linear segment price in power generation economic dispatch matlab code
in line 17 AND SOME OTHER LINES THIS ERROR COMES: Variable F appears to change size on every loop iteration I DONT KNOW HOW TO SOLVE THAT!

1 件のコメント

Walter Roberson
Walter Roberson 2018 年 2 月 6 日
Note that this is a warning, not an error. It hints that your code is less efficient than it could be. For large arrays the cost of growing the array can end up being a major slowdown.

サインインしてコメントする。

回答 (2 件)

José-Luis
José-Luis 2017 年 1 月 2 日

3 投票

The solution is to pre-allocate:
Do
results = ones(1,10)
for ii = 1:10
results(ii) = ii;
end
instead of:
for ii = 1:10
begin_for_error(ii) = ii;
end
I didn't go through your code so you'll have to modify to fit your needs.

5 件のコメント

Jan
Jan 2018 年 2 月 6 日
And replace
q=1;
for i=1:N % converts the column matrix "x" to a rectangular shape
% for easy row addition on next loop
for j=1:N
X(i,j)=x(q);
q=q+1;
end
end
by:
X = reshape(x, N, N);
Learn to use vectorized expressions. The fact that they are faster might not matter in your case, but they are cleaner and leaner: Less chances for typos and easier to write and maintain.
Sultan Al-Hammadi
Sultan Al-Hammadi 2020 年 5 月 19 日
編集済み: Sultan Al-Hammadi 2020 年 5 月 19 日
What if you don't want to allocate specific values to the enteries of the array? I got the same one while I was trying to plot an animation. Is there a way of optimising it as the exceution of my code is noticably slow.
Rik
Rik 2020 年 5 月 19 日
Since the goal is to overwrite them anyway, does it matter what value it starts out with?
You could consider pre-allocating with NaN, as many functions will ignore those.
If you want more specific advice, you should post this as a separate question.
Kelvin Ling Chia Hiik
Kelvin Ling Chia Hiik 2020 年 6 月 28 日
thanks Jose Luis

サインインしてコメントする。

Sohan
Sohan 2023 年 10 月 17 日

0 投票

function waterfallspect(s, fs, spectsize, spectnum)
%WATERFALLSPECT Display spectrum of signal s as a 3-D plot.
% s - input signal, use wavload to load a WAV file
% fs - Sampling frequency (Hz).
% spectsize - FFT window size
% spectnum - number of windows to analyze
frequencies = [0:fs/spectsize:fs/2];
offset = floor((length(s)-spectsize)/spectnum);
for i=0:(spectnum-1)
start = i*offset;
A = abs(fft(s((1+start):(start+spectsize))));
magnitude(:,(i+1)) = A(1:spectnum/2+1);
end
waterfall(frequencies, 0:(spectnum-1), magnitude');
xlabel('frequency');
ylabel('time');
zlabel('magnitude');

1 件のコメント

Walter Roberson
Walter Roberson 2023 年 10 月 17 日
編集済み: Walter Roberson 2023 年 10 月 17 日
No, this would have the same warning as the original code, about a variable changing size in every iteration, so this code would not fix the problem that the original poster had.
Is there a reason you did not pre-allocate the matrix magnitude ?

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2017 年 1 月 2 日

編集済み:

2023 年 10 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by