Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % HW4
- % hW4 info
- %
- % 3. Use of continue: Consider the following script:
- % x = [1 -2 3 -4 5 0 2];
- % total = 0;
- % for i = 1:length(x) == 7
- % if x(i)<0, continue; end
- % total=total+x(i);
- % end
- %
- % a. What is the purpose of the script? What is the final value of "total"?
- % b. Can you rewrite the script using the while-loop instead?
- %
- %
- %
- % 9. 給定一向量 A,請寫一段程式 useIf01.m,利用 if-then-else 指令來依元素值不同 而印出不同訊息。舉例而言,當 A=[-1, 1, 0, 2+i] 時,你的程式碼應印出:
- % ◦A(1) = -1 是negative number
- % ◦A(2) = 1 是正數
- % ◦A(3) = 0 是零
- % ◦A(4) = 2+i 是複數
- % 用下列 A 來測試你的程式,並將結果印出來:A = randn(20,1)+(rand(20,1)>0.7)*sqrt(-1);
- %
- % 10. 重複上一題,程式碼名稱為 useSwitch01.m,但是必須使用 switch-case-otherwise 指令來完成。
- % ------
- % use function in 9,10 and no Chinese.
- function hw4()
- disp('problem 3a')
- fprintf('purpose: sum of positive number\n')
- x = [1 -2 3 -4 5 0 2];
- total=0;
- p=0;
- while( p < numel(x) )
- p=p+1;
- if x(p)<0, continue; end
- total=total+x(p);
- end
- fprintf('\nproblem 3b\n')
- fprintf('total = %g\n',total);
- %-------------------------------------------------
- fprintf('\nproblem 9: This is useIf01.m\n')
- resultStr=useIf01( randn(20,1)+(rand(20,1)>0.7)*sqrt(-1) );
- disp(resultStr);
- %-------------------------------------------------
- fprintf('\nproblem 9: This is useIf02.m, another method\n')
- resultStr=useIf02( randn(20,1)+(rand(20,1)>0.7)*sqrt(-1) );
- disp(resultStr);
- %-------------------------------------------------
- fprintf('\nproblem 10: This is useSwitch01.m\n')
- resultStr=useSwitch01( randn(20,1)+(rand(20,1)>0.7)*sqrt(-1) );
- disp(resultStr);
- end
- function str=useIf01(a)
- tag=cell(size(a));
- tag( a>0 & imag(a)==0) = {'positive number'};
- tag( a==0 & imag(a)==0) = {'zero'};
- tag( a<0 & imag(a)==0) = {'negative number'};
- tag( imag(a)~=0 ) = {'complex numbers'};
- str=[];
- for n=1:numel(a)
- if strcmp(tag(n),'positive number')
- str=[str,sprintf('a(%d) = %g is a %s\n',n ,a(n) ,tag{n} ) ];
- elseif strcmp(tag(n),'zero')
- str=[str,sprintf('a(%d) = %g is a %s\n',n ,a(n) ,tag{n} ) ];
- elseif strcmp(tag(n),'negative number')
- str=[str,sprintf('a(%d) = %g is a %s\n',n ,a(n) ,tag{n} ) ];
- elseif strcmp(tag(n),'complex numbers')
- str=[str,sprintf('a(%d) = %s is a %s\n',n ,num2str(a(n)) ,tag{n} ) ];
- else
- str=[str,sprintf('a(%d) not a numbers\n',n ,a(n) ) ];
- end
- end
- end
- function str=useIf02(a)
- % other method
- str=[];
- for n=1:numel(a)
- if isreal(a(n))
- if a(n) > 0
- str=[str,sprintf('a(%d) = %g is a positive number\n',n ,a(n) ) ];
- elseif a(n) < 0
- str=[str,sprintf('a(%d) = %g is a negative number\n',n ,a(n) ) ];
- else
- str=[str,sprintf('a(%d) = %g is a zero\n',n ,a(n) ) ];
- end
- elseif isnumeric(a(n))
- str=[str,sprintf('a(%d) = %s is a complex numbers\n',n ,num2str(a(n)) ) ];
- else
- str=[str,sprintf('a(%d) is not a complex numbers\n',n ,a(n) ) ];
- end
- end
- end
- function str=useSwitch01(a)
- tag=cell(size(a));
- tag( a>0 & imag(a)==0) = {'positive number'};
- tag( a==0 & imag(a)==0) = {'zero'};
- tag( a<0 & imag(a)==0) = {'negative number'};
- tag( imag(a)~=0 ) = {'complex numbers'};
- str=[];
- for n=1:numel(a)
- switch tag{n}
- case 'positive number'
- str=[str,sprintf('a(%d) = %g is a %s\n',n ,a(n) ,tag{n} ) ];
- case 'zero'
- str=[str,sprintf('a(%d) = %g is a %s\n',n ,a(n) ,tag{n} ) ];
- case 'negative number'
- str=[str,sprintf('a(%d) = %g is a %s\n',n ,a(n) ,tag{n} ) ];
- case 'complex numbers'
- str=[str,sprintf('a(%d) = %s is a %s\n',n ,num2str(a(n)) ,tag{n} ) ];
- otherwise
- str=[str,sprintf('a(%d) is not a complex numbers\n',n ,a(n) ) ];
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment