实现排序功能

时间:2015-05-05 13:56:55

标签: matlab function

我希望实现一个MATLAB函数,该函数将三个标量作为输入并返回一个行向量。

最小的输入标量应该是第一个元素,最大的标量应该是行向量的最后一个元素。我无法使用sort功能。

在我的代码下面,但它不起作用。

function nw = new(s,m,l)

if s >= m || m >= l
    if s == m && m == l
        nw = [s m l]
    else
        s > m && m > l
        nw = [s m l]
        if  s == m && m>l
            nw = [l m s];
        else s > m && m == l
            nw = [l m s];
        end
    end
else s <= m || m <= l
    if s < m && m < l
        nw = [s m l];
    else s == m && m < l
        if s < m && m == l
            nw = [l m s]
        else
            if s > m && s < l
                nw = [l m s]
            end
        end       
end
end

4 个答案:

答案 0 :(得分:2)

这个怎么样?

function nw = new(s,m,l)
v = [s m l];
nw = [min(v), median(v), max(v)];

任何人都可以想到这种情况不起作用的情况吗?我不能......

答案 1 :(得分:1)

这可能不是最有效(最快)的方式,但不时玩这个很有趣。

此功能将对您的3个数字进行排序:

function nw = new(s,m,l)

v = [s m l ] ; %// group all variable in one array

if all(v == v(1)) %// check just in case they are all the same value
    nw = v ;
else
    [~,ix(1)] = min( v ) ; %// assign the index of the smallest value
    [~,ix(3)] = max( v ) ; %// assign the index of the largest value
    ix(2) = 6 - sum(ix) ;  %// find the middle index by difference (because cumsum([1 2 3]) = 6 )
    nw = v(ix) ;           %// assign the output vector based on indices collected
end

答案 2 :(得分:-1)

当我找到你的时候,你可以使用sort:

function nw = new(s,m,l)
  nw = sort([s,m,l]);
end

答案 3 :(得分:-1)

void static2::on_pushButtonNext_clicked()
{
if (incrementer == 0)
{
    int tot_size = text.size();
    const char *str;  
    QByteArray array; //http://www.qtcentre.org/threads/22711-Converting-QString-to-char-array
    array = text.toLatin1();
    str = array.data();
}

//write an if statement here that prevents from running past the total stream. run if less than num_events_dec to end.
if (incrementer*num_events_dec <= tot_size - num_events_dec)
{
    incrementer++;
    ui->lcdNumber->display(incrementer);  //updates display

    int step = (incrementer-1)*num_events_dec; //this is the code that goes bit by bit on the stream
    for (int i = step; i < step+num_events_dec; i++){

        //PLACE COLOR SETTING COMMANDS HERE
        //http://stackoverflow.com/questions/2749798/qlabel-set-color-of-text-and-background
        //http://www.qtcentre.org/archive/index.php/t-5944.html

        if (str[i] == '1'){
            //make label background green

            //qDebug() << "high";

            //QLabel* color = new QLabel; //[num_events_dec]; //allocates an array of objects called color which is of the user specified size

            /*QString color = "color_" + QString::number(i);
            qDebug() << i;
            qDebug() << color;
             */

            /* As of right now i have string values that are of the proper name
             * I need those same names as Qlabels in order for ui->xxx to recognize them
             * The class mainwindow has an object ui which has an object called color_[i]
             */

            //ui->color->setStyleSheet("background-color: rgb(0, 255, 0);");
            switch (i-step){
            case 0: ui->color_0->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 1: ui->color_1->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 2: ui->color_2->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 3: ui->color_3->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 4: ui->color_4->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 5: ui->color_5->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 6: ui->color_6->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 7: ui->color_7->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 8: ui->color_8->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 9: ui->color_9->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 10: ui->color_10->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 11: ui->color_11->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 12: ui->color_12->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 13: ui->color_13->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 14: ui->color_14->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 15: ui->color_15->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 16: ui->color_16->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 17: ui->color_17->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 18: ui->color_18->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 19: ui->color_19->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 20: ui->color_20->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 21: ui->color_21->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 22: ui->color_22->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 23: ui->color_23->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 24: ui->color_24->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 25: ui->color_25->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 26: ui->color_26->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 27: ui->color_27->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 28: ui->color_28->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 29: ui->color_29->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 30: ui->color_30->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            case 31: ui->color_31->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
            } //end switch case
         } //end if

        else if (str[i] == '0'){
            //make label background red

            //qDebug() << "low";

            switch (i-step){
            case 0: ui->color_0->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 1: ui->color_1->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 2: ui->color_2->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 3: ui->color_3->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 4: ui->color_4->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 5: ui->color_5->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 6: ui->color_6->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 7: ui->color_7->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 8: ui->color_8->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 9: ui->color_9->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 10: ui->color_10->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 11: ui->color_11->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 12: ui->color_12->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 13: ui->color_13->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 14: ui->color_14->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 15: ui->color_15->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 16: ui->color_16->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 17: ui->color_17->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 18: ui->color_18->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 19: ui->color_19->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 20: ui->color_20->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 21: ui->color_21->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 22: ui->color_22->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 23: ui->color_23->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 24: ui->color_24->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 25: ui->color_25->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 26: ui->color_26->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 27: ui->color_27->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 28: ui->color_28->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 29: ui->color_29->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 30: ui->color_30->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
            case 31: ui->color_31->setStyleSheet("background-color: rgb(255, 0, 0);"); break;

            } //end switch case

        } //end else if

        else {
            QMessageBox::warning(this,"Error", "A non-binary number has been read -- Check input stream");

        } //end else
    } //end for loop
} //end if

else  //dont allow to increment
{
    QMessageBox::warning(this,"Error", "Stream Ended, No more data to view");
} //end else