MATLAB小函数:找出矩阵中某一列(行)元素全相等(不等)的列(行)所在下标及对应元素值
作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/
1. 找出矩阵中某一列(行)元素全相等的列(行)所在下标及对应元素值
给定一个矩阵,一列一列找,如果某一列中全部元素都是相同的值,则返回该列的id号。例如:aa矩阵中第2列元素值全是2.3,第5列元素值全是7,则返回列下标值2, 5与相应的元素值2.3, 7。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | aa = 5.0000 2.3000 4.0000 8.0000 7.0000 6.0000 2.3000 4.0000 6.0000 7.0000 8.0000 2.3000 1.0000 4.0000 7.0000 8.0000 2.3000 6.0000 4.0000 7.0000 1.0000 2.3000 5.0000 9.0000 7.0000 0 2.3000 1.0000 5.0000 7.0000 3.5000 2.3000 6.4000 8.6000 7.0000 output = 2.0000 2.3000 5.0000 7.0000 |
1.1 MATLAB程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function output=Column_consistent(data) % MATLAB 找出矩阵中某一列元素全相等的列所在的下标及对应的元素值 % Input: 数据集 % Output:列下标号 元素值 [~, dim]= size (data); index=[]; value_col=[]; for i =1:dim result_frequency=tabulate(data(:, i )); value=result_frequency(result_frequency(:, 3)==100); if ~ isempty (value) index=[index; i ]; value_col=[value_col; value]; end end output=[index value_col]; |
1.2 结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | >> aa=[5 2.3 4 8 7; 6 2.3 4 6 7; 8 2.3 1 4 7; 8 2.3 6 4 7; 1 2.3 5 9 7;0 2.3 1 5 7; 3.5 2.3 6.4 8.6 7] aa = 5.0000 2.3000 4.0000 8.0000 7.0000 6.0000 2.3000 4.0000 6.0000 7.0000 8.0000 2.3000 1.0000 4.0000 7.0000 8.0000 2.3000 6.0000 4.0000 7.0000 1.0000 2.3000 5.0000 9.0000 7.0000 0 2.3000 1.0000 5.0000 7.0000 3.5000 2.3000 6.4000 8.6000 7.0000 >> output=Column_consistent(aa) output = 2.0000 2.3000 5.0000 7.0000 |
注意:如果是找出矩阵中某一行元素全相等的行所在下标及对应元素值,只需把数据转置一下就可以,例如,bb数据集中第1行元素全为1,第4行元素全为0,则返回行下标1, 4和相应的元素值1, 0。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | >> bb=[1 1 1 1; 2 5 4 7; 3 5 4 8; 0 0 0 0; 6 8 4 5] bb = 1 1 1 1 2 5 4 7 3 5 4 8 0 0 0 0 6 8 4 5 >> output=Column_consistent(bb') output = 1 1 4 0 |
2. 统计矩阵中某一列(行)元素全不等的列(行)的个数及所在下标
2.1 MATLAB程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | function [index, sum_no_consistent]=Column_no_consistent(data) % MATLAB 统计矩阵中某一列元素全不一致的列的个数及所在下标 % Input: % data: 数据集 % Output: % index: 列下标号 元素值 % sum_no_consistent: 统计矩阵中某一列元素全不一致的列的个数 [Num, dim]= size (data); index=[]; value_col=[]; for i =1:dim result_frequency=tabulate(data(:, i )); value=( sum (result_frequency(:, 3)~=0)==Num); if value~=0 index=[index; i ]; value_col=[value_col; value]; end end sum_no_consistent= sum (value_col); |
2.2 结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | >> cc=[5 2.3 4 8 7; 6 2.3 4 6 7; 8 2.3 1 4 7; 8.1 2.3 6 4.1 7; 1 2.3 5 9 7;0 2.3 1 5 7; 3.5 2.3 6.4 8.6 7] cc = 5.0000 2.3000 4.0000 8.0000 7.0000 6.0000 2.3000 4.0000 6.0000 7.0000 8.0000 2.3000 1.0000 4.0000 7.0000 8.1000 2.3000 6.0000 4.1000 7.0000 1.0000 2.3000 5.0000 9.0000 7.0000 0 2.3000 1.0000 5.0000 7.0000 3.5000 2.3000 6.4000 8.6000 7.0000 >> [index, sum_no_consistent]=Column_no_consistent(cc) index = 1 4 sum_no_consistent = 2 |
cc数据集里面有2列元素全不相同,分别是第1和第4列。同样,如果想统计有多少行元素全不相同,只需转置数据即可,例如,将cc改为cc'。
2020-06-04
标签:
MATLAB
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 中的改进
· 当数据爆炸遇上SQL Server:优化策略全链路解析
· 记录一次线上问题排查:JDK序列化问题
· 微服务之间有哪些调用方式?
· 记一次SQL隐式转换导致精度丢失问题的排查
· 分享5款开源、美观的 WinForm UI 控件库
· DeepSeek又在节前放大招!
· 领域驱动的事实与谬误 一 DDD 与 MVC
· 从零到一搭建一个前端工具函数库
· 4种插值算法