2012-02-19 44 views
-4

我做这个项目,但我的讲师希望它更加简单..我试图改变命令,但它出错..可以请帮我..给一些线索或任何..我真的很感激,如果u能帮助我..这是问题,下面是命令。要确定最高,最低和平均根据用户自己的选择通过MATLAB程序的平均两个向量的价值..如何简化这个程序

close all 
    clear all 
    clc 

    disp('Welcome to my program.'); 
    disp(' '); 
    disp('instruction:'); 
    disp(' a) Enter the number of column of a vector.'); 
    disp(' b) Next, enter the element for vector A and vector B.'); 
    disp(' c) Then, select your option of what do you want to find.'); 
    disp(' '); 
    column = input (' Enter the number of column of a vector: '); 
    disp(' ') 
    disp(' Enter the element for vector A '); 
     for count=1:column 
     disp(['A (',num2str(count), ')=']); 
     A(count)=input(' '); 
    end 
    disp(' ') 
    disp(' Enter the element for vector B'); 
    for count=1:column 
     disp(['B(',num2str(count),')=']); 
     B(count)=input(' '); 
    end 
    disp(' ') 
    disp(['Vector A is [',num2str(A),')']); 
    disp(['Vector B is [',num2str(B),')']); 
    disp(' ') 
    disp('What do you want to find?'); 
    disp(' ') 
    disp('1-find the highest value'); 
    disp('2-find the lowest value'); 
    disp('3-find the average value'); 
    choose=input('Choose:'); 
    disp(' ') 

    while choose >3 
     disp('Sorry, please enter the right input!'); 
     choose=input('choose:'); 
    end 

    disp('Your result:') 
    disp(' ') 
    fprintf('number of column:%2.0f\n',column); 
    disp(['vector A:[',num2str(A),']']); 
    disp(['vector B:[',num2str(B),']']); 

    if choose ==1 
     disp('choice: find the highest value'); 
    elseif choose==2 
     disp('choice: find the lowest value'); 
    elseif choose==3 
     disp('choice: find the average value'); 
    end 
    switch choose 
     case 1 
      A = max(A); 
      B = max(B); 
      result=max(A,B); 
     case 2 
      A = min (A); 
      B = min (B); 
      result=min(A,B); 
     case 3 
      A = mean (A); 
      B = mean (B); 
    end 

    disp(['Vector A:',num2str(A)]); 
    disp(['Vector B:',num2str(B)]); 
    if choose==1 
     disp(['the highest value: ',num2str(result),'']); 
    else if choose==2 
      disp(['the lowest value:',num2str(result),'']); 
     end 
    end 
+2

“我试图改变命令,但它出错”:那你试试,它是怎样去错了吗? – 2012-02-19 05:48:33

回答

3

我推荐了几个变化:

  1. 问问你的教授是什么“简单”的模样。
  2. 模块化 - 将代码放在您调用的较小方法中。
  3. 减少提示输入。我反对那些关于最小,最大和平均的东西。只是为了善良而把它们全部拿出来。
  4. 可见代码有点混乱。我会有更小的方法;保持代码清洁。
  5. 循环提示输入是单向的;从文件读取将是另一个。我会给自己从哪里获得价值的选择。保持与计算分开。
+0

你是什么意思'保持代码清洁'? – niyan 2012-02-19 04:47:21

+0

我认为所有显示的东西都是视觉混乱。将计算与所有I/O和提示分开。 – duffymo 2012-02-19 04:54:40

2

考虑MATLAB的内置功能是如何构造的。用户只需通过传递附加参数来选择该案例。不是通过显示屏提示用户,在函数调用下面有一组注释,用户可以通过调用“help yourfunctionname”来请求注释。

所以,非常粗略的,你应该写一个看起来像这样的函数:

function f = yourfunction(A,B,i) 
%A and B are input vectors 
%i = 1 for max, i = 2 for min, i = 3 for mean 

'the rest of your code' 
+0

如果您将'i'重命名为'calc_type',并允许它成为'min | max | mean'中的字符串,它会更加用户友好。然后使用'strcmp()'来确定使用哪个计算。 (请注意:不要使用'=='运算符比较字符串 - 它不会像你想象的那样工作。) – 2012-02-19 05:47:16

+2

你看到那个人的代码?婴儿的步骤。 – prototoast 2012-02-19 06:01:53

+0

你的观点很好。 ;) – 2012-02-19 06:24:28

3

我会去像这样的东西:

function out = mergevectors(x,y,method) 
%MERGEVECTORS Determine max, min, or mean value 
% MERGEVECTORS(X, Y, 'CHOICE') determines basic statistics between two 
% vectors. X and Y are any identically sized vectors. 'METHOD' is one 
% of: 'max','min','mean'. 
% 
%Examples: 
% x = [1 2 3 4]; 
% y = [4 3 2 1]; 
% mergevectors(x, y,'max') 
% mergevectors(x, y,'min') 
% mergevectors(x, y,'mean') 


%Error handling 
error(nargchk(3,3,nargin)); 
if ~isequal(size(x), size(y)) 
    error('X and Y must be identically sized') 
end 
if ~ischar(method) 
    error('Method must be one of ''max'', ''min'', ''mean'''); 
end 

%Processing 
switch lower(method) 
    case 'max' 
     out = max(x, y); 
    case 'min' 
     out = min(x, y); 
    case 'mean' 
     out = (x + y)/2; 
    otherwise 
     error('Method must be one of ''max'', ''min'', ''mean'''); 
end 

一些自我放纵的评论:

  • 请避免语句,如:清除所有;关闭所有; CLC;我明白为什么有这么多人这样做,但如果我使用了你的工具,我有机会在工作空间中拥有我想要保留的东西。这个声明是不需要的。如果您担心工作区中的杂散变量,则关键字function工作得很好。

  • 没有必要来引导用户虽然打字在载体的冗长方法。 Matlab命令窗口已经带有适用于输入向量的工具。 (例如,[])。

  • 一般情况下,错误形式交往应当语气完全中立。不是“!”的。任何错误至少是程序员的一半错误,他们找不到使用所提供输入的方法或提供更好的文档。

  • 在提供的解决方案中,初始注释块用作文档,可通过help mergevectors向用户提供。但是,一旦用户成为专家用户,文档就不再需要,而且不会侵入。

  • 避免交互输入如input只要你能。 (也就是说,总是避免交互式输入。)编写一个有用的函数的重点是让你(或其他人)在忘记它的工作方式后能够很好地使用它,并且可以用它来构建更好,更高级的函数。您很少想要通过不断的输入请求来减慢这个过程。

    对于这个学习的例子,这一点可能并不明显,但如果您曾经写过复杂的代码段(不管语言如何),这是一个需要记住的点。