Iterations on field conditions of a structure array

1 回表示 (過去 30 日間)
SS
SS 2019 年 10 月 7 日
コメント済み: Piyush Dubey 2019 年 11 月 11 日
Hi. I am working with a structure array S (1 X 50,000) with 10 fields. I want to find few paramters based on few conditions. Is there a simple way to do such iterations shown below using a loop and save them in a structure?
f1 = [S.f1];
f2 = [S.f2];
f3 = [S.f3];
%% ITERATION 1:
idx1 = f1 > 0 & f1 <=10;
% Parameter 1
num1 = numel(f1(idx1));
avg1 = mean(f2(idx));
sd1 = std(f2(idx));
% Parameter 2
idx2 = f3 > 0;
idx = idx1 & idx2;
pos_num1 = numel(f1(idx));
pos_avg1 = mean(f2(idx));
pos_sd1 = std(f2(idx));
% Parameter 3
idx2 = f3 < 0;
idx = idx1 & idx2;
neg_num1 = numel(f1(idx));
neg_avg1 = mean(f2(idx));
neg_sd1 = std(f2(idx));
%% ITERATION 2:
idx1 = f1 > 10 & f1 <=20;
% Parameter 1
num2 = numel(f1(idx1));
avg2 = mean(f2(idx));
sd2 = std(f2(idx));
% Parameter 2
idx2 = f3 > 0;
idx = idx1 & idx2;
pos_num2 = numel(f1(idx));
pos_avg2 = mean(f2(idx));
pos_sd2 = std(f2(idx));
% Parameter 3
idx2 = f3 < 0;
idx = idx1 & idx2;
neg_num2 = numel(f1(idx));
neg_avg2 = mean(f2(idx));
neg_sd2 = std(f2(idx));
% so on till 20 interations.
The result should be saved like:
Result.num=[num1,num2,num3.........,num20];
Result.pos_num=[pos_num1,pos_num2,pos_num3,.....,pos_num20];
Result.neg_num=[neg_num1,neg_num2,neg_num2,......,neg_num20];
Result.avg=[avg1,avg2,avg3,.....avg20];
.. so on.
Any help will be highly appreciated.

採用された回答

Piyush Dubey
Piyush Dubey 2019 年 10 月 10 日
Hi,
It is not very clear from your explanation what you are trying to accomplish. If you are trying to iterate using indices that satisfy some conditions, you should make a matrix with each column containing indices of vector 'f1' that satisfy the required conditions. For example,
idx_list = [(f1 > 0 & f1 <= 10) (f1 > 10 & f1 <= 20) (f1 > 20 & f1 <= 30)];
idx_list_size = 3;
num_list = zeros(idx_list_size, 1);
And iterate in the following way:
for i=1:idx_list_size
num_list(i) = numel(f1(idx_list(:, i)));
end
  2 件のコメント
SS
SS 2019 年 11 月 10 日
Thanks for the answer. How to save them in the structure?
Piyush Dubey
Piyush Dubey 2019 年 11 月 11 日
You can save a list to a structure as follows:
Result.num_list = num_list;

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by