how can i store a value of a calculation in a loop

2 ビュー (過去 30 日間)
Ketan Bharucha
Ketan Bharucha 2017 年 10 月 5 日
コメント済み: jean claude 2017 年 10 月 8 日
Hi I have the following loop.
for k=1:length(hplid)
x=hplid(k);
q=sort(latency(hpl==x));
size(q);
figure;
plot(q,(1:numel(q))/numel(q))
title(['HPL ', num2str(x), ' Latency']);
xlabel('Seconds');
ylabel('% Percentile');
quant=interp1((1:numel(q))/numel(q),q,[.9 .95 .99]);
disp( '90%, 95% and 99% latency');
disp(quant);
end
how can I store the value of quants to an array everytime the loop is exectued. Right quants only store the calculation of the last iteration of the loop.
Thank you
  1 件のコメント
Jan
Jan 2017 年 10 月 5 日
I have formatted your code using the "{} Code" button.

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

回答 (2 件)

Jan
Jan 2017 年 10 月 5 日
編集済み: Jan 2017 年 10 月 5 日
Store the vectors in a matrix using the loop counter as index:
quant = zeros(length(hplid), 3); % Pre-allocate!!!
for k = 1:length(hplid)
...
quant(k, :) = interp1((1:numel(q))/numel(q),q,[.9 .95 .99]);
disp(quant(k, :));
end
Note: The line "size(q);" is useless.
  2 件のコメント
Ketan Bharucha
Ketan Bharucha 2017 年 10 月 5 日
I get the following error when I use your solution? any idea?
The following error occurred converting from duration to double: Undefined function 'double' for input arguments of type 'duration'. To convert from durations to numeric, use the SECONDS, MINUTES, HOURS, DAYS, or YEARS functions.
Jan
Jan 2017 年 10 月 6 日
@Ketan: Please post the complete error message and the relevant part of the code. I can neither guess, which line causes the error, nor which variable has the type "duration". Note that the message contains a hint about the solution already.

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


jean claude
jean claude 2017 年 10 月 5 日
try this put it just before end of the loop
quantvalue=[];
quantvalue=[quantvalue quant];
  2 件のコメント
Walter Roberson
Walter Roberson 2017 年 10 月 5 日
This works, but is not efficient. Each time something is added on to the end of quantvalue, MATLAB has to look for new memory long enough to store the combined results, and copy the existing quantvalue over and then copy in the new values, and then throw away the existing quantvalue. When possible, it is much more efficient to allocate the memory to store all of the results and write them in as you go.
jean claude
jean claude 2017 年 10 月 8 日
yes that's totally right it's better to preallocate!

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by