查找数组

时间:2015-09-19 15:14:20

标签: arrays matlab

如何在MATLAB中找到1D数组的2个最大值的索引?我是一个包含不同分数列表的数组,我想打印出2个最高分。

3 个答案:

答案 0 :(得分:3)

您可以使用sort,如@LuisMendo建议:

[B,I] = sort(array,'descend');

这为您提供了变量arrayB的排序版本以及I中从最高到最低排序的原始位置的索引。因此,B(1:2)为您提供了最高的两个值,I(1:2)为您提供了array中的索引。

答案 1 :(得分:2)

我会选择O(k*n)解决方案,k是您要查找的最大值的数量,而不是O(n log n)

x = [3 2 5 4 7 3 2 6 4];
y = x; %// make a copy of x because we're going to modify it
[~, m(1)] = max(y);
y(m(1)) = -Inf;
[~, m(2)] = max(y);

m =

   5   8

这仅在k小于log n时才有效。事实上,如果k>=3我会把它放在一个循环中,这可能会冒犯一些人的感情。 ;)

答案 2 :(得分:1)

获取两个最大元素的索引:使用sort的第二个输出来获取排序的索引,然后选择最后两个:

html {
    width:100%;
    height:100%;
}

body {
    height: 100%;
    width: 100%;
    margin:0 auto;
    padding: 0;
    font-size: 1em;
}

* {
    box-sizing: border-box;
    -webkit-font-feature-settings: "liga" 0;
    font-variant-ligatures: none;
    -webkit-font-variant-ligatures: no-common-ligatures;
}





/*
 * index.html page
 */

.wrapper {
    position: absolute;
    max-width: 45%;
    max-height:45%;
    top:50%;
    left:50%;
}

#dot {
    position:relative;
    max-width:100%;
    max-height:100%;
    margin-top:-50%;
    margin-left:-50%;
}

/* Dot animation */

 @keyframes pulse {
    from {
        width: 70px;
        height: 70px;
    }
    to {
        width: 90px;
        height: 90px;
    }
}

#dot {
    animation: pulse 1200ms ease-in-out infinite alternate;
}


/* 
 * Hello.html Page
 */ 


.main-container {
  display: flex;
    height: 100vh;
    width: 100vw;
    justify-content: center;
    align-items: center;  
}

/*
 * before/after img inline
 * Navigation
 */

.nav__link a{
    visibility: hidden;
}
.small_dot {
    vertical-align:middle;
    padding-left: 10px;
}

.side-bar__nav{
    position: absolute;
    padding: 10px;
    right: 0;
    top: 50%;
    transform: translateY(-50%);

}

.side-bar__nav ul li{
    list-style: none;
    text-align: right;
    margin-right: 50px;
    padding-top: 15px;
}
.side-bar__nav ul li img {
    margin-bottom: 5px;
}

.small_dot:hover {

}

.side-bar__nav a {

    text-decoration: none;
    color: rgba(0,0,0,0);
    font-family: 'Lato', sans-serif;
    font-size: 1.1em;
    -webkit-transition: all 1s ease-in;
    -moz-transition: all 1s ease-in;
    -o-transition: all 1s ease-in;
    transition: all 1s ease-in;

}

.side-bar__nav:hover a {
   color: rgba(0, 0, 0, 1);
}


.main-header {
    text-align: center;
  }

footer { 
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
}

 .footer__text {
    text-align: center;
 }

 .languages {
 }



/*
 * Typography
 */

h1 {
    font-weight: 100;
    font-size: 5.5em;
    font-family: 'Lato', sans-serif;
}

h2 {
    font-family: 'Noto Serif', serif;
    font-weight: 700;
    font-size: 3.5em;

}

img {
    max-width: 100%;
}

@media (max-width: 600px) {
    h1 {
        font-size: 3em;
    }

    h2 {
        font-size: 2.5em;
    }

    .side-bar__nav {
        display: none;
    }
}

@media (max-width: 1228px) {
    .side-bar__nav {
        display: none;
    }
}

在这种情况下,

x = [3 2 5 4 7 3 2 6 4];
[~, ind] = sort(x);
result = ind(end-1:end);