1、%求一个矩阵中的每个元素是否是奇数,相当于MATLAB的函数 isprimefunction res fctr=is_prime(n)res=ones(size(n);fctr=ones(size(n);for K=1:length(n(:)cur_n=n(K);res(K)fctr(K)=is_a_prime(cur_n);end%判断单个数是否是奇数function res fctr=is_a_prime(n)res=true;for I=2:floor(sqrt(n)if mod(n,I)=0 fctr=I;res=false;return;endend%主程序subplot(3,1,1
2、);A=2:100;stem(A,A.*is_prime(A),.-)subplot(3,1,2);A=2:1000;stem(A,A.*is_prime(A),.-)subplot(3,1,3);A=2:5000;stem(A,A.*is_prime(A),.-)salary=linspace(0,10000,20);plot(salary,get_revenue(salary),x-);grid on;subplot(2,1,2);salary=linspace(10000,200000,20);plot(salary,get_revenue(salary),x-);grid on;fun
3、ction revenues=get_revenue(salaries)revenues=ones(size(salaries);for I=1:length(salaries(:)revenues(I)=get_a_revenue(salaries(I);endfunction revenue=get_a_revenue(salary)revenue=0;if salary 1600 return;end%-1600 2100salary=salary-1600;if salary (2100-1600)revenue=revenue+salary*0.05;return;else reve
4、nue=revenue+(2100-1600)*0.05;end%-2100 3600salary=salary-(2100-1600);if salary (3600-2100)revenue=revenue+salary*0.10;return;else revenue=revenue+(3600-2100)*0.10;end%-3600 6600%-6600 21600%-21600 41600%-41600 61600%-61600 81600%-81600 101600%-81600 101600salary=salary-(81600-61600);if salary salary
5、);I =pre_I(1)-1;if(I=0)return;endrevenues=pre_fee(I)+(salary-starts(I)*rates(I);A=1:7 9 100I=1;J=length(A);while I A(I)A(I+1)=n;return;end A(I+1)=A(I);endA(I)=n;function A=insert2(A,n)I=find(An);if(length(I)=0)A=A n;return;endI=I(1);A=A(1:I-1)n A(I:length(A);11 11 2 11 3 3 11 4 6 4 11 5 10 10 5 11 6
6、 15 20 15 6 1function yanhui2(n)A=1;disp(A);for I=2:n A=A 0+0 A;disp(A);endfunction yanhui1(n)A=1;disp(A);for I=2:n B=1 A;A=A 0;for J=2:I B(J)=A(J-1)+A(J);end disp(B);A=B;end 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84
7、36 9 1function v pos=get_min1(A)pos=;v=min(min(A);M N=size(A);for I=1:M for J=1:N if(v=A(I,J)pos=pos;I J;end endend94 50 17 45 077 62 13 78 5873 79 21 28 5486 44 10 22 6599 52 14 90 31function v pos=saddle(A)v=;pos=;line_min=min(A);N=size(A,1);for I=1:N Js=find(A(I,:)=line_min(I);for K=1:length(Js)J
8、=Js(K);if A(I,J)=max(A(:,J)v=v A(I,J);pos=pos;I J;end endendM=3;N=5;%生成一个M=3行N=5列%有规律的矩阵%11 12 13 14 15%21 22 23 24 25%31 32 33 34 35A=(1:M)*ones(1,N)*10 +ones(M,1)*(1:N);disp(A);disp_rank_k(A,3);function disp_rank_k(A,K)M N=size(A);if K M|KN disp(Input error);returnendIs=combnk(1:M,K);Js=combnk(1:N,K);cnt=1;for I=1:size(Is,1)II=Is(I,:);for J=1:size(Js,1);JJ=Js(J,:);disp(sprintf(%3d:,cnt);cnt=cnt+1;disp(A(II,JJ);end