QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 1152|回复: 1
打印 上一主题 下一主题

Matlab---数据排序-2

[复制链接]
字体大小: 正常 放大
回帖奖励 1 点体力 回复本帖可获得 1 点体力奖励! 每人限 1 次

413

主题

36

听众

1854

积分

升级  85.4%

  • TA的每日心情
    开心
    2019-9-18 21:55
  • 签到天数: 258 天

    [LV.8]以坛为家I

    社区QQ达人

    群组2015国赛冲刺

    群组2016美赛公益课程

    群组国赛讨论

    群组第三届数模基础实训

    群组Matlab讨论组

    跳转到指定楼层
    1#
    发表于 2015-9-16 22:48 |只看该作者 |倒序浏览
    |招呼Ta 关注Ta
    答案:
    这个程序必须提示使用者提供输入数据,对其进行排序,并输出排序结要。这个程序的设计过程如下:
    1.陈述问题我们刚才没有指定要排序的数据类型。如果数据是数字,那么问题的陈述如下。开发一个程序,它能够读取在命令窗口中输入的任意类型的数字,用独立的自定义函数对读取的值进行排序,并在命令窗口写出排序结果。
    2.定义输入输出量这个程序的输入值是在命令窗口键入的数字值。这个程序的输出量是写在命令窗口中的排序结果。
    3.设计算法这个问题可以分解为三大步:

    Read the input data into an array
    Sort the data in ascending order
    Write the sorted data


    第一大步是读取数据。我们必须提示使用者输入输入数据的个数,然后读取数据。因为我们知道所要读取的数的确切个数,所以可以用for循环主读取合适的数据。它的伪代码如下:

    Prompt user for the number of data values
    Read the number of data values
    Preallocate an input array
    for ii = 1:number of values
           Promptfor next value
           Readvalue
    end


    下一步,我们必须要用独立的函数对数据进行排序。我们需要对数据进行naval-1次浏览,每一次找出一个最小值。我们将用一个指针来寻找每一次浏览的最小值。一量最小值被找到,如果它不在列表的顶端,它就与列表顶端的元素进行交换。伪代码如下:

    for ii = 1:nvals -1
    % Find the minimum value in a(ii) through a(nvals)
        iptr ii
        for jj =ii +1 to nvals
            ifa(jj) < a(iptr)
               iptr a(iptr)
            end
        end
    % iptr now points to the min value, so swap a(iptr)
    % with a(ii) if iptr ~= ii.
        if ii ~=iptr
            temp a(ii)
            a(ii) a(iptr)
           a(iptr) temp
        end
    end


    最后一步是输出排序结果。这个步骤的伪代码不需要重复。最终的伪代码是这三大步伪代码的联合。
    4.把伪代码转化为MATLAB语言选择性排序的MATLAB代码如下所示:

    function out = ssort(a)
    %SSORT Selection sort data in ascending order
    % Function SSORT sorts a numeric data set into
    % ascending order. Note that the selection sort
    % is relatively inefficient. DO NOT USE THIS
    % FUNCTION FOR LARGE DATA SETS. Use MATLAB's
    % "sort" function instead.
    % Define variables:
    % a        --Input array to sort
    % ii       --Index variable
    % iptr     --Pointer to min value
    % jj       --Index variable
    % nvals    --Number of values in "a"
    % out      --Sorted output array
    % temp     --Temp variable for swapping
    % Record of revisions:
    % Date     Programmer      Description ofchange
    % ====     ==========      =====================
    % 12/19/98  S. J. Chapman   Original code
    % Get the length of the array to sort
    nvals = size(a,2);
    % Sort the input array
    for ii = 1:nvals-1
    % Find the minimum value in a(ii) through a(n)
        iptr =ii;
        for jj =ii+1:nvals
            ifa(jj) < a(iptr)
               iptr = jj;
            end
        end
    % iptr now points to the minimum value, so swapa(iptr)
    % with a(ii) if ii ~= iptr.
        if ii ~=iptr
            temp= a(ii);
            a(ii)= a(iptr);
           a(iptr) = temp;
        end
    end
    % Pass data back to caller
    out = a;


    调用选择性排序函数的程序如下:

    % Script file: test_ssort.m
    %
    % Purpose:
    % To read in an input data set, sort it intoascending
    % order using the selection sort algorithm, and to
    % write the sorted data to the Command window. This
    % program calls function "ssort" to dothe actual
    % sorting.
    %
    % Record of revisions:
    % Date     Programmer      Description ofchange
    % ====     ==========     =====================
    % 12/19/98  S. J. Chapman   Original code
    %
    % Define variables:
    % array        --Input data array
    % ii           --Index variable
    % nvals        --Numberof input values
    % sorted       --Sorted data array
    % Prompt for the number of values in the data set
    nvals = input('Enter number of values to sort: ');
    % Preallocate array
    array = zeros(1,nvals);
    % Get input values
    for ii = 1:nvals
        %Promptfor next value
        string =['Enter value ' int2str(ii) ': '];
       array(ii)=input(string);
    end
    % Now sort the data
    sorted = ssort(array);
    % Display the sorted result.
    fprintf('\nSorted data:\n');
    for ii = 1:nvals
        fprintf('%8.4f \n',sorted(ii));
    end


    5.检测程序
    为了检测这个程序,我们应当创建一个输入数据集,并运行这个程序。这个数据集应当包含由正负数混合组成,还有至少包括一个完全一样的值,以观察在这些情况下程序是否正常工作。

    >> test_ssort
    Enter number of values to sort: 6
    Enter value 1: -5
    Enter value 2: 4
    Enter value 3: -2
    Enter value 4: 3
    Enter value 5: -2
    Enter value 6: 0
    Sorted data:
      -5.0000
      -2.0000
      -2.0000
       0.0000
       3.0000
       4.0000


    对于我们检测的数据,程序给出了正确的结果。注意从正数到负数,还有重复值,这个程序工作正常。




    zan
    转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
    数学中国版主团队!

    413

    主题

    36

    听众

    1854

    积分

    升级  85.4%

  • TA的每日心情
    开心
    2019-9-18 21:55
  • 签到天数: 258 天

    [LV.8]以坛为家I

    社区QQ达人

    群组2015国赛冲刺

    群组2016美赛公益课程

    群组国赛讨论

    群组第三届数模基础实训

    群组Matlab讨论组

    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2025-5-21 07:10 , Processed in 0.494497 second(s), 55 queries .

    回顶部