フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Pre-allocate memory gives zeros all the time, how to avoid?

2 ビュー (過去 30 日間)
want2know
want2know 2013 年 11 月 7 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
In the beginning of a function, we have to pre-allocate memory by doing something like this: integ_signal=zeros(5,4);
Using the below code as an example, if I comment out this line: integ_signal=zeros(5,4);, I will get the desired answer which is 10.1 instead of 0.1. But, in Matlab Function block, I have to pre-allocate memory, how could I pre-allocate memory at the same time still won't assign zero to the integ_signal all the time?
integ_signal(1,1)=10;
integ_signal=zeros(5,4);
a=zeros(10);
arr=zeros(5,4);
count=2;
a=1;
integ_signal(count,1)= integ_signal(count-1,1);
arr(count,1) = integ_signal(count,1);
arr(count,1) = arr(count,1) + abs(I(1,a)-I(1,a+1));
(Desired answer, without pre-allocating memory) >> arr arr =
0 0 0 0
10.1000 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
(Wrong answer, with pre-allocating memory) >> arr
arr =
0 0 0 0
0.1000 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 7 日
In your Matlab function, declare a variable arr as persistent
persistent arr
if isempty(arr)
arr=zeros(5,4)
end
You write
a=zeros(10),
a=1
The result is a=1 , then the line a=zeros(10) is unnecessary
  1 件のコメント
want2know
want2know 2013 年 11 月 7 日
ok, i tried this in Matlab workspace, but I got this Error using persistent A PERSISTENT declaration is only allowed in a M-file function. So, I was thinking to try this in my m-file, but due to the error I posted earlier (<http://www.mathworks.co.uk/matlabcentral/answers/105217-subscripted-assignment-dimension-mismatch-size-1-x-1-size-1-x)>, I could not check the outcome of using persistent yet.

Simon
Simon 2013 年 11 月 7 日
Hi!
Maybe your missing 10 because the first two rows are in the wrong order?

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by