sppmg

matlab-hw4

Apr 11th, 2015
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.81 KB | None | 0 0
  1. % HW4
  2. %  hW4 info
  3. %  
  4. %  3. Use of continue: Consider the following script:
  5. %  x = [1 -2 3 -4 5 0 2];
  6. %  total = 0;
  7. %  for i = 1:length(x)  == 7
  8. %   if x(i)<0, continue; end
  9. %   total=total+x(i);
  10. %  end
  11. %  
  12. %   a. What is the purpose of the script? What is the final value of "total"?
  13. %   b. Can you rewrite the script using the while-loop instead?
  14. %  
  15. %  
  16. %  
  17. %  9. 給定一向量 A,請寫一段程式 useIf01.m,利用 if-then-else 指令來依元素值不同 而印出不同訊息。舉例而言,當 A=[-1, 1, 0, 2+i] 時,你的程式碼應印出:
  18. %  ◦A(1) = -1 是negative number
  19. %  ◦A(2) = 1 是正數
  20. %  ◦A(3) = 0 是零
  21. %  ◦A(4) = 2+i 是複數
  22. %  用下列 A 來測試你的程式,並將結果印出來:A = randn(20,1)+(rand(20,1)>0.7)*sqrt(-1);
  23. %  
  24. %  10. 重複上一題,程式碼名稱為 useSwitch01.m,但是必須使用 switch-case-otherwise 指令來完成。
  25. %  ------
  26. %  use function in 9,10 and no Chinese.
  27. function hw4()
  28.     disp('problem 3a')
  29.     fprintf('purpose: sum of positive number\n')
  30.  
  31.     x = [1 -2 3 -4 5 0 2];
  32.     total=0;
  33.     p=0;
  34.     while( p < numel(x) )
  35.         p=p+1;
  36.         if x(p)<0, continue; end
  37.         total=total+x(p);
  38.  
  39.     end
  40.     fprintf('\nproblem 3b\n')
  41.     fprintf('total = %g\n',total);
  42.  
  43.     %-------------------------------------------------
  44.     fprintf('\nproblem 9: This is useIf01.m\n')
  45.     resultStr=useIf01( randn(20,1)+(rand(20,1)>0.7)*sqrt(-1) );
  46.     disp(resultStr);
  47.     %-------------------------------------------------
  48.     fprintf('\nproblem 9: This is useIf02.m, another method\n')
  49.     resultStr=useIf02( randn(20,1)+(rand(20,1)>0.7)*sqrt(-1) );
  50.     disp(resultStr);
  51.     %-------------------------------------------------
  52.     fprintf('\nproblem 10: This is useSwitch01.m\n')
  53.     resultStr=useSwitch01( randn(20,1)+(rand(20,1)>0.7)*sqrt(-1) );
  54.     disp(resultStr);
  55. end
  56.  
  57. function str=useIf01(a)
  58.     tag=cell(size(a));
  59.     tag( a>0 & imag(a)==0) = {'positive number'};
  60.     tag( a==0  & imag(a)==0) = {'zero'};
  61.     tag( a<0  & imag(a)==0) = {'negative number'};
  62.     tag( imag(a)~=0 ) = {'complex numbers'};
  63.  
  64.     str=[];
  65.     for n=1:numel(a)
  66.         if strcmp(tag(n),'positive number')
  67.             str=[str,sprintf('a(%d) = %g is a %s\n',n ,a(n) ,tag{n} ) ];
  68.         elseif strcmp(tag(n),'zero')
  69.             str=[str,sprintf('a(%d) = %g is a %s\n',n ,a(n) ,tag{n} ) ];
  70.         elseif strcmp(tag(n),'negative number')
  71.                 str=[str,sprintf('a(%d) = %g is a %s\n',n ,a(n) ,tag{n} ) ];
  72.         elseif strcmp(tag(n),'complex numbers')
  73.                 str=[str,sprintf('a(%d) = %s is a %s\n',n ,num2str(a(n)) ,tag{n} ) ];
  74.         else
  75.                 str=[str,sprintf('a(%d) not a numbers\n',n ,a(n) ) ];
  76.         end
  77.     end
  78. end
  79.  
  80. function str=useIf02(a)
  81.     % other method
  82.     str=[];
  83.     for n=1:numel(a)
  84.         if isreal(a(n))
  85.             if a(n) > 0
  86.                 str=[str,sprintf('a(%d) = %g is a positive number\n',n ,a(n) ) ];
  87.             elseif a(n) < 0
  88.                 str=[str,sprintf('a(%d) = %g is a negative number\n',n ,a(n) ) ];
  89.             else
  90.                 str=[str,sprintf('a(%d) = %g is a zero\n',n ,a(n) ) ];
  91.             end
  92.         elseif isnumeric(a(n))
  93.             str=[str,sprintf('a(%d) = %s is a complex numbers\n',n ,num2str(a(n)) ) ];
  94.         else
  95.             str=[str,sprintf('a(%d) is not a complex numbers\n',n ,a(n) ) ];
  96.         end
  97.     end
  98. end
  99.  
  100. function str=useSwitch01(a)
  101.    
  102.     tag=cell(size(a));
  103.     tag( a>0 & imag(a)==0) = {'positive number'};
  104.     tag( a==0  & imag(a)==0) = {'zero'};
  105.     tag( a<0  & imag(a)==0) = {'negative number'};
  106.     tag( imag(a)~=0 ) = {'complex numbers'};
  107.  
  108.     str=[];
  109.     for n=1:numel(a)
  110.         switch tag{n}
  111.             case 'positive number'
  112.                 str=[str,sprintf('a(%d) = %g is a %s\n',n ,a(n) ,tag{n} ) ];
  113.             case 'zero'
  114.                 str=[str,sprintf('a(%d) = %g is a %s\n',n ,a(n) ,tag{n} ) ];
  115.             case 'negative number'
  116.                 str=[str,sprintf('a(%d) = %g is a %s\n',n ,a(n) ,tag{n} ) ];
  117.             case 'complex numbers'
  118.                 str=[str,sprintf('a(%d) = %s is a %s\n',n ,num2str(a(n)) ,tag{n} ) ];
  119.             otherwise
  120.                 str=[str,sprintf('a(%d) is not a complex numbers\n',n ,a(n) ) ];
  121.         end
  122.     end
  123. end
Advertisement
Add Comment
Please, Sign In to add comment