博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sort_nat
阅读量:4042 次
发布时间:2019-05-24

本文共 2879 字,大约阅读时间需要 9 分钟。

function [cs,index] = sort_nat(c,mode)%sort_nat: Natural order sort of cell array of strings.% usage:  [S,INDEX] = sort_nat(C)%% where,%    C is a cell array (vector) of strings to be sorted.%    S is C, sorted in natural order.%    INDEX is the sort order such that S = C(INDEX);%% Natural order sorting sorts strings containing digits in a way such that% the numerical value of the digits is taken into account.  It is% especially useful for sorting file names containing index numbers with% different numbers of digits.  Often, people will use leading zeros to get% the right sort order, but with this function you don't have to do that.% For example, if C = {'file1.txt','file2.txt','file10.txt'}, a normal sort% will give you%%       {'file1.txt'  'file10.txt'  'file2.txt'}%% whereas, sort_nat will give you%%       {'file1.txt'  'file2.txt'  'file10.txt'}%% See also: sort% Version: 1.4, 22 January 2011% Author:  Douglas M. Schwarz% Email:   dmschwarz=ieee*org, dmschwarz=urgrad*rochester*edu% Real_email = regexprep(Email,{'=','*'},{'@','.'})% Set default value for mode if necessary.if nargin < 2	mode = 'ascend';end% Make sure mode is either 'ascend' or 'descend'.modes = strcmpi(mode,{'ascend','descend'});is_descend = modes(2);if ~any(modes)	error('sort_nat:sortDirection',...		'sorting direction must be ''ascend'' or ''descend''.')end% Replace runs of digits with '0'.c2 = regexprep(c,'\d+','0');% Compute char version of c2 and locations of zeros.s1 = char(c2);z = s1 == '0';% Extract the runs of digits and their start and end indices.[digruns,first,last] = regexp(c,'\d+','match','start','end');% Create matrix of numerical values of runs of digits and a matrix of the% number of digits in each run.num_str = length(c);max_len = size(s1,2);num_val = NaN(num_str,max_len);num_dig = NaN(num_str,max_len);for i = 1:num_str	num_val(i,z(i,:)) = sscanf(sprintf('%s ',digruns{i}{:}),'%f');	num_dig(i,z(i,:)) = last{i} - first{i} + 1;end% Find columns that have at least one non-NaN.  Make sure activecols is a% 1-by-n vector even if n = 0.activecols = reshape(find(~all(isnan(num_val))),1,[]);n = length(activecols);% Compute which columns in the composite matrix get the numbers.numcols = activecols + (1:2:2*n);% Compute which columns in the composite matrix get the number of digits.ndigcols = numcols + 1;% Compute which columns in the composite matrix get chars.charcols = true(1,max_len + 2*n);charcols(numcols) = false;charcols(ndigcols) = false;% Create and fill composite matrix, comp.comp = zeros(num_str,max_len + 2*n);comp(:,charcols) = double(s1);comp(:,numcols) = num_val(:,activecols);comp(:,ndigcols) = num_dig(:,activecols);% Sort rows of composite matrix and use index to sort c in ascending or% descending order, depending on mode.[unused,index] = sortrows(comp);if is_descend	index = index(end:-1:1);endindex = reshape(index,size(c));cs = c(index);

转载地址:http://jvxdi.baihongyu.com/

你可能感兴趣的文章
今日互联网关注(写在清明节后):每天都有值得关注的大变化
查看>>
”舍得“大法:把自己的优点当缺点倒出去
查看>>
[今日关注]鼓吹“互联网泡沫,到底为了什么”
查看>>
[互联网学习]如何提高网站的GooglePR值
查看>>
[关注大学生]求职不可不知——怎样的大学生不受欢迎
查看>>
[关注大学生]读“贫困大学生的自白”
查看>>
[互联网关注]李开复教大学生回答如何学好编程
查看>>
[关注大学生]李开复给中国计算机系大学生的7点建议
查看>>
[关注大学生]大学毕业生择业:是当"鸡头"还是"凤尾"?
查看>>
[茶余饭后]10大毕业生必听得歌曲
查看>>
gdb调试命令的三种调试方式和简单命令介绍
查看>>
C++程序员的几种境界
查看>>
VC++ MFC SQL ADO数据库访问技术使用的基本步骤及方法
查看>>
VUE-Vue.js之$refs,父组件访问、修改子组件中 的数据
查看>>
Vue-子组件改变父级组件的信息
查看>>
Python自动化之pytest常用插件
查看>>
Python自动化之pytest框架使用详解
查看>>
【正则表达式】以个人的理解帮助大家认识正则表达式
查看>>
性能调优之iostat命令详解
查看>>
性能调优之iftop命令详解
查看>>