How to create answers to a calculation in an array in a forloop ?

1 回表示 (過去 30 日間)
iceskating911
iceskating911 2022 年 6 月 29 日
コメント済み: Voss 2022 年 6 月 29 日
Hello! I'm still fairly new to matlab and have a question to ask.
I have a dataset (labeled "data) and are using two columns of the dataset to do calcuations. The following forloop gets me the answers I am looking for:
a = Data(:,1);
b = Data(:,2);
for l = 1 : length(edges)-1
indexes = a > edges(l) & a <= edges(l+1);
BinSums(l) = sum(b(indexes));
end
Which is a 1 x 16 double with a value in each column!
Now what I want to do, is to do this same calculation 1000 times, so in the end I have an array of 1000 x 16 with each row being a new set of values.
Note: This is only part of a longer and more complicated code that inputs new data for a and b every iteration. i just want to know how I save each iteration into a new array that is 1000 x 16 if that makes sense.
I can provide more detail if needed, thank you so much for the help!!!

採用された回答

Voss
Voss 2022 年 6 月 29 日
Nbins = numel(edges)-1;
BinSums = zeros(1000,Nbins);
for jj = 1:1000
% ...
% different a and b each time
a = Data(:,1);
b = Data(:,2);
% ...
for ii = 1 : Nbins
indexes = a > edges(ii) & a <= edges(ii+1);
BinSums(jj,ii) = sum(b(indexes));
end
end
  7 件のコメント
iceskating911
iceskating911 2022 年 6 月 29 日
Okay, thank you for all the help!
Voss
Voss 2022 年 6 月 29 日
You're welcome!

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2022 年 6 月 29 日
Use discretize to generate the index vector then pass those index / group indices into groupsummary as the grouping variable and your data as the data variable on which to operate.

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by