How can i start 'for loop' initializing from zero and have decimal increment??

11 ビュー (過去 30 日間)
madhavireddy
madhavireddy 2020 年 4 月 30 日
コメント済み: pooja sudha 2021 年 6 月 19 日
array = [x,y];
for i=0:0.01:1
y_ord(i)=nanmax(array(array(:,1)>=i & array(:,1)<(i+0.01), 2));
end

回答 (1 件)

Steven Lord
Steven Lord 2020 年 4 月 30 日
Your for loop is fine. But there's no such thing as elements 0 or 0.1 of an array in MATLAB (the first element of an array in MATLAB is element 1), so your attempt to assign into those elements in y_ord will fail.
You could create that vector and iterate over the elements of that vector:
edges = 0:0.01:1;
for whichedge = 1:numel(edges)
ii = edges(whichedge)
y_ord(whichedge) = someFunctionOfii;
end
I used ii as the variable because i already has a meaning in MATLAB.
Though now that I look at your code, you can probably just use groupsummary. The "Group Operations with Vector Data" example on that page is closest to what you're trying to do, only you'd specify numbers (the edges of your bins) instead of 'dayname' to define the bins and 'max' or @max as the summary function instead of 'mean'.
  1 件のコメント
pooja sudha
pooja sudha 2021 年 6 月 19 日
Hey steven, your code is really helpful.
Can you please tell me if I want to write a matrix of ii of dimension numel(edges)*numel(edges). How can I write that?

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

カテゴリ

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