Hey guys, I want to save xMax3 and yMax3 in two different matrixes, and it has values in each iteration, however with my code, it only saves values different from zero in the first and last iteration, where is the error. Thanks in advance
counter=1;
surveillance_reshaped = reshape(Surveillance_signal, 100000, 2, []);
reference_reshaped = reshape(Reference_signal, 1000, 2, []);
sz = size(surveillance_reshaped);
for i = 1:sz(3)
r = reference_reshaped(:,1,i);
s = surveillance_reshaped(:,1,i);
[afmag3,doppler3] = ambgfun(r,s,Fs,[250000 250000],'Cut','Delay');
afmag3(afmag3>1 )= 1;
[pks3,index3] = max(afmag3);
xMax3 = doppler3(index3);
yMax3 = pks3;
yMax3(:,i)=yMax3;
xMax3(:,i)=xMax3;
counter=counter+1
end

 採用された回答

dpb
dpb 2022 年 7 月 12 日

1 投票

"... where is the error. (?)"
...
xMax3 = doppler3(index3);
yMax3 = pks3;
...
overwrites/redefines the two variables in their entirety each pass through the loop, thus eliminating the previous value. MATLAB assignment without subscripting means "the whole thing".

7 件のコメント

Miguel Albuquerque
Miguel Albuquerque 2022 年 7 月 12 日
How can I solve this?
dpb
dpb 2022 年 7 月 12 日
Always use the indices in the LHS expression...preallocate the arrays before beginning the loop.
Miguel Albuquerque
Miguel Albuquerque 2022 年 7 月 12 日
I didnt understand how to do that in my code, I ve already done that I think
Stephen23
Stephen23 2022 年 7 月 12 日
編集済み: Stephen23 2022 年 7 月 13 日
"I ve already done that I think"
You do... but dpb is also correct that you are overwriting the variable names on every loop iteration. Have a look at these four lines:
xMax3 = doppler3(index3); % <- overwrites xMax3 on every itation...
yMax3 = pks3;
yMax3(:,i)=yMax3;
xMax3(:,i)=xMax3; % <- ... yet here you try to index into it on the LHS.
That is unlikely to be very useful code: after that first line, what are you indexing into?
Miguel Albuquerque
Miguel Albuquerque 2022 年 7 月 12 日
In the first line im only calculating xMax3, but then with the last line I wanted to save it in each column, how can I do that so?
dpb
dpb 2022 年 7 月 12 日
You're calculating doppler3 every pass, but to preallocate you need to know before the loop how long it is -- or you can continue to let the two arrays be dynamically allocated each pass through the loop--although it is far less efficient, unless the size of the loop is very large it will likely not be a big-enough performance hit to matter significantly.
So, on that presumption, before the loop begins insert
xMax3=[]; yMax3=[]; % create variables; empty initially; just a reference exists
Then, in the loop replace the four lines with only two...
xMax3(:,i)=doppler3(index3);
yMax3(:,i)=pks3;
and you're done. Reference the column every time; the initial empty assignment will allow the undefined dimension of the length of the column to be the initial size of the RHS vector w/o specifically being preallocated to an exact size.
Miguel Albuquerque
Miguel Albuquerque 2022 年 7 月 12 日
Thanks a lot dpb, I have this gigantic task to do, and this little help, is so much valuable.

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

その他の回答 (0 件)

カテゴリ

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

製品

リリース

R2021b

質問済み:

2022 年 7 月 12 日

編集済み:

2022 年 7 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by