找到结构体数组某个属性满足条件的其他属性\或满足属性条件的样本

找到结构体数组某个属性满足条件的其他属性

描述:每一个样本是通过一个结构体的形式进行描述。多个样本通过一个结构体数组进行描述。其中每一个样本都有一个唯一的标识属性index。我们的目的是找到:某个属性满足条件的,所有样本的标示index。

例如,在下面的实验中,我们找到年龄大于21的所有人的index,或者所示唯一的id。
这里写图片描述

clc;clearvars;close all;
n=3;
id=[12 3 8];
name={'x','l','h'};
age=[20 22 24];
index=[2 1 3];
%% 初始化一个struct数组
s_info=repmat(struct('index',[],'id',[],'name',[],'age',[]),3,1);

for i=1:n
    % index表示样本的id,每一个样本有唯一的一个Id
    s_info(i).index=index(i);
    s_info(i).id=id(i);
    s_info(i).name=name{i};
    s_info(i).age=age(i);
end

%% 找到年龄大于21的所有样本索引
%% 注意,这里的两个中括号都是必须的,其利用了两种属性,一个类似于结构体排序的,然后要加上中括号,另外一个是将结构体转换为数组的,然后要加上中括号
s_ind=[s_info([s_info.age]>21).index];

disp('over...');

结果:

s_ind=[1 3]

满足属性条件的样本

% the behavior of struct likes data table in database
student_table = repmat(struct('id',[],'name',[],'sex',[],'math_score',[]),5,1);

student_table(1) = struct('id','2012011601','name','zhangsan','sex','男','math_score',96);
student_table(2) = struct('id','2012011602','name','lisi','sex','女','math_score',90);
student_table(3) = struct('id','2012011603','name','wanger','sex','男','math_score',86);
student_table(4) = struct('id','2012011604','name','mazi','sex','男','math_score',70);
student_table(5) = struct('id','2012011605','name','kongsheng','sex','男','math_score',92);

% select * where sex='nan'&&math_score>90
excel_students = student_table(([student_table.sex]=='男')&([student_table.math_score]>=90));

for i = 1:length(excel_students)
    disp(excel_students(i));
end
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值