找到结构体数组某个属性满足条件的其他属性\或满足属性条件的样本
2016-10-19 10:47
204 查看
找到结构体数组某个属性满足条件的其他属性
描述:每一个样本是通过一个结构体的形式进行描述。多个样本通过一个结构体数组进行描述。其中每一个样本都有一个唯一的标识属性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
相关文章推荐
- 找到满足条件的数组
- 百度面试题:找到满足条件的数组
- 百度面试题找到满足条件的数组
- 小小c#算法题 - 1 - 找出数组中满足条件的两个数
- 数组中满足条件的项 保存到另一个数组中 并移除
- 百度面试题 找到满足条件的数
- C语随机产生20个正整数存入数组a中,且每个数均在1000-9999之间(包含1000和9999)。对数组进行排序,要求按每个数的后三位的大小进行升序排列,然后取出满足此条件的前10个数放入数组b中,
- 随机产生20个正整数存入数组a中,且每个数均在1000-9999之间(包含1000和9999)。对数组进行排序,要求按每个数的后三位的大小进行升序排列,然后取出满足此条件的前10个数放入数组b中,如果
- 找出数组中满足条件的所有组合!
- 编程之美 2.12 快速寻找满足条件的两个数 解法三证明 (算法导论 第二版 2.3-7 在n个元素的集合S中找到两个和为x的元素)
- C语言关于数组、指针、函数和结构体的其他知识
- 随机产生20个正整数存入数组a中,且每个数均在1000-9999之间(包含1000和9999)。对数组进行排序,要求按每个数的后三位的大小进行升序排列,然后取出满足此条件的前10个数放入数组b中,如果
- 一个关于数组中满足条件的元素选择、及再次排序得c语言函数。
- 求出千位数上的数加百位数上的数等于十位数上的数加个位数上的数的个数cnt,再把所有满足条件的四位数依次存入数组b中,然后对数组b中的四位数按从大到小的顺序进行排序。
- 求出所有这些四位数是素数的个数cnt,再把所有满足此条件的四位数依次存入数组b中,然后对数组b中的四位数按从小到大的顺序进行排序
- java实现树的节点之间距离;向下递归,注意找到一个满足条件的节点要break
- 删除SDE中要素,依据属性条件删除SDE某要素集中满足要求的要素
- 依次从数组a中取出一个四位数,如果该四位数连续大于该四位数以后的5个数,且该数是奇数,则把这个四位数按从小到大的顺序存入数组b中,并计算满足上述条件的四位数的个数cnt。
- ie8中图片设置max-width属性满足一定的条件会导致消失
- 随机产生20个正整数存入数组a中,且每个数均在1000-9999之间(包含1000和9999)。对数组进行排序,要求按每个数的后三位的大小进行升序排列,然后取出满足此条件的前10个数放入数组b中,如果