如何将多个行向量与矩阵进行比较?

时间:2017-06-23 16:14:35

标签: matlab vector equality

我有24 x 3 matrix “point1”我还有另外5个1x3 row vectors。我想要的是将所有5个不同的行向量与“point1”的每一行进行比较,以便查看5个向量中的任何一个是否在“point1”中有相应的行等于它们,然后返回“point1”中该行的索引。我已经能够使用以下代码执行此操作,但我正在寻求更简单和优雅(可能没有循环?)解决方案。

point1 = [7.5   4   5
          8.5   4   5
         9.5    4   5
         10.5   4   5
         11.5   4   5
         7      4   5.5
         12     4   5.5
         6.5    4   6
        12.5    4   6
           6    4   6.5
          13    4   6.5
         5.5    4   7
        13.5    4   7
           5    4   7.5
          14    4   7.5
           5    4   8.5
          14    4   8.5
           5    4   9.5
          14    4   9.5
           5    4   10.5
          14    4   10.5
           5    4   11.5
          14    4   11.5
         5.5    4   12];

fN = [8, 4.5, 5];
fS = [8, 3.5, 5];
fE = [8.5, 4, 5];     
bN = [7, 4.5, 5];  
bT = [7, 4, 5.5];

for ii = 1:size(point1, 1)         
 indx(ii) = isequal(point1(ii,:),fN(:)') | isequal(point1(ii,:),fS(:)') | isequal(point1(ii,:),fE(:)') | isequal(point1(ii,:),bN(:)') | isequal(point1(ii,:),bT(:)') 
 pIndx = find(indx)      
end

返回:

pIndx = [2 6];

谢谢你们!

2 个答案:

答案 0 :(得分:6)

您可以使用ismember with the 'rows' flag搜索矢量和数据矩阵之间的交点。

使用上面的示例,您可以最简单地将concatenate查询向量转换为一个矩阵并将其用作输入:

<div class="form-group">
  <div class="row">
    <div class="col-md-2">
    </div>
    <div class="col-md-9">
      <h5 id="textColor">Consecteteur Eliptois eliot</h5>
      <div id="carouselIndicators1" class="carousel slide" data-ride="carousel">
        <ol class="carousel-indicators">
          <li data-target="#carouselIndicators1" data-slide-to="0" class="active"></li>
          <li data-target="#carouselIndicators1" data-slide-to="1"></li>
          <li data-target="#carouselIndicators1" data-slide-to="2"></li>
        </ol>
        <div class="carousel-inner" role="listbox">
          <div class="carousel-item active">
            <img class="d-block img-fluid" src="carouselbox.jpg" alt="First slide" style="padding-right:6px;">
            <!-- Static Header -->
            <div class="header-text hidden-xs">
              <div class="col-md-12 text-center">
                <h5>
                  <span><strong>Image1</strong></span><br /> Lorem Ipsum
                </h5>
              </div>
            </div>
            <!-- /header-text -->
            <img class="d-block img-fluid" src="carouselbox.jpg" alt="First slide" style="padding-right:6px;">
            <!-- Static Header -->
            <div class="header-text2 hidden-xs" id="secondslide">
              <div class="col-md-12 text-center">
                <h5>
                  <span><strong>Image1</strong></span><br /> Lorem Ipsum
                </h5>
              </div>
            </div>
            <!-- /header-text -->
            <img class="d-block img-fluid" src="carouselbox.jpg" alt="First slide">
            <!-- Static Header -->
            <div class="header-text3 hidden-xs" id="secondslide">
              <div class="col-md-12 text-center">
                <h5>
                  <span><strong>Image1</strong></span><br /> Lorem Ipsum
                </h5>
              </div>
            </div>
            <!-- /header-text -->
          </div>
          <div class="carousel-item">
            <img class="d-block img-fluid" src="carouselbox.jpg" alt="Second slide" style="padding-right:6px;">
            <!-- Static Header -->
            <div class="header-text hidden-xs" id="secondslide">
              <div class="col-md-12 text-center">
                <h5>
                  <span><strong>Image1</strong></span><br /> Lorem Ipsum
                </h5>
              </div>
            </div>
            <!-- /header-text -->
            <img class="d-block img-fluid" src="carouselbox.jpg" alt="Second slide" style="padding-right:6px;">
            <!-- Static Header -->
            <div class="header-text2 hidden-xs" id="secondslide">
              <div class="col-md-12 text-center">
                <h5>
                  <span><strong>Image1</strong></span><br /> Lorem Ipsum
                </h5>
              </div>
            </div>
            <!-- /header-text -->
            <img class="d-block img-fluid" src="carouselbox.jpg" alt="Second slide">
            <!-- Static Header -->
            <div class="header-text3 hidden-xs" id="secondslide">
              <div class="col-md-12 text-center">
                <h5>
                  <span><strong>Image1</strong></span><br /> Lorem Ipsum
                </h5>
              </div>
            </div>
            <!-- /header-text -->
          </div>
          <div class="carousel-item">
            <img class="d-block img-fluid" src="carouselbox.jpg" alt="Third slide" style="padding-right:6px;">
            <!-- Static Header -->
            <div class="header-text hidden-xs" id="secondslide">
              <div class="col-md-12 text-center">
                <h5>
                  <span><strong>Image1</strong></span><br /> Lorem Ipsum
                </h5>
              </div>
            </div>
            <!-- /header-text -->
            <img class="d-block img-fluid" src="carouselbox.jpg" alt="Third slide" style="padding-right:6px;">
            <!-- Static Header -->
            <div class="header-text2 hidden-xs" id="secondslide">
              <div class="col-md-12 text-center">
                <h5>
                  <span><strong>Image1</strong></span><br /> Lorem Ipsum
                </h5>
              </div>
            </div>
            <!-- /header-text -->
            <img class="d-block img-fluid" src="carouselbox.jpg" alt="Third slide">
            <!-- Static Header -->
            <div class="header-text3 hidden-xs" id="secondslide">
              <div class="col-md-12 text-center">
                <h5>
                  <span><strong>Image1</strong></span><br /> Lorem Ipsum
                </h5>
              </div>
            </div>
            <!-- /header-text -->
            <a href="" style="margin-top:55px;margin-left:12px;"><strong>Next</strong></a>
          </div>
        </div>
        <a class="carousel-control-prev" href="#carouselIndicators1" role="button" data-slide="prev">
          <span class="carousel-control-prev-icon" aria-hidden="true"></span>
          <span class="sr-only">Previous</span>
        </a>
        <a class="carousel-control-next" href="#carouselIndicators1" role="button" data-slide="next">
          <span class="carousel-control-next-icon" aria-hidden="true"></span>
          <span class="sr-only">Next</span>
        </a>
      </div>
      <!--end of carousel indicators-->
    </div>
  </div>
</div>

返回:

test = find(ismember(point1, vertcat(fN, fS, fE, bN, bT), 'rows'))

或者,如果单个结果很重要,您可以单独进行查询:

test =

     2
     6

答案 1 :(得分:0)

您可以尝试以下方法:

[find(all(fN == point1, 2)), ...
find(all(fS == point1, 2)), ...
find(all(fE == point1, 2)), ...
find(all(bN == point1, 2)), ...
find(all(bT == point1, 2))]