赞
踩
function [op]=Find(subset,bigset) %Find([1,1,1],[3,4,16,4,4,2,9,9,9,9,9,9,1,1,1]) %寻找子集在母集合中的位置,注意返回的位置顺序和subset的顺序一样 %注意输入的是行向量 %注意这个函数只是适用于特定问题!!!!!!!!!! staMat=Repsta(subset.'); if isempty(staMat)==1%说明subset没有重复的元素 op=zeros(1,length(subset)); for uu=1:length(subset) op(1,uu)=find(bigset==subset(uu)); end else%说明有重复的元素 Repnums=staMat(1,:);%重复的元素 op=zeros(1,length(subset)); for vv=1:length(Repnums) op(1,subset==Repnums(vv))=(find(bigset==Repnums(vv))); end for ww=1:length(subset) if ismember(subset(ww),Repnums)==0%不是重复的元素 op(1,ww)=(find(bigset==subset(ww))); end end end %%%%%代码测试%%%%%%%%% if unique(bigset(op)-subset)==0 disp('结果正确') end end function [op]=Repsta(col_num) %Repsta([1;2;3;4;4;1;1;1;6;7;9;0;0;12;13;12;3]) %输入一个列向量col_num %输出一个矩阵op %op的大小为两行n列 %第一行是重复的元素 %第二行是对应元素重复的次数 %如果没有重复的元素,返回空集矩阵 unique_col_num=unique(col_num); len_uni_col_num=length(unique_col_num); len_col_num=length(col_num); if len_uni_col_num==len_col_num op=[]; else count=0; for xx=1:len_uni_col_num tp=unique_col_num(xx); Site=find(col_num==tp);%位置 len_Site=length(Site); if len_Site>1 count=count+1; op(1:2,count)=[tp;len_Site]; end end end end
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。