第1个回答 推荐于2018-05-06
function [pos,row] =find_matrix(a);
handle =fopen('pos.txt','w');
dim =size(a);
C =reshape(a,1,[]);
row =sort(C,'descend');
fprintf(handle,'After sort : %g \r\n\r\n',row);
for index =1:length(C)
pos = find(a==C(index));
if mod(pos,dim(1))==0
rowtp =dim(1);
else
rowtp =mod(pos,dim(1));
end
if pos/dim(1)-mod(pos/dim(1),1)>0
cltp =pos/dim(1)-mod(pos/dim(1),1);
else
cltp =1;
end
fprintf(handle,'%g is at : row = %g ,column =%g \r\n\r\n',C(index),rowtp,cltp);
end
fclose(handle);
end本回答被网友采纳
第2个回答 2020-03-21
1、字符串排序应该用sortrows函数。
2、你的变量a使用cell数组感觉非常别扭,处理起来麻烦很多,不如直接用结构数组(那样代码中的两次转换都可以省去)。
示例代码:
a{1}.text='bb';
a{2}.text='aa';
a{1}.data=4;
a{2}.data=8;
%
转为结构数组
a=[a{:}];
%
按字符串排序
[b,idx]=sortrows({a.text}');
a=a(idx);
%
转回cell数组
a=mat2cell(a,1,ones(1,length(a)));
%
查看结果
a{1}
a{2}