Iterations on field conditions of a structure array

1 view (last 30 days)
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.

Accepted Answer

Piyush Dubey
Piyush Dubey on 10 Oct 2019
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 Comments
SS
SS on 10 Nov 2019
Thanks for the answer. How to save them in the structure?
Piyush Dubey
Piyush Dubey on 11 Nov 2019
You can save a list to a structure as follows:
Result.num_list = num_list;

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!