Iterations on field conditions of a structure array
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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
2019-11-11
You can save a list to a structure as follows:
Result.num_list = num_list;
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!