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
Piyush Dubey
Piyush Dubey 2019년 11월 11일
You can save a list to a structure as follows:
Result.num_list = num_list;

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by