两个不同长度的矢量产品

时间:2017-12-14 03:06:03

标签: r

如果我有两个不同长度的矢量。

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container" style="margin-top: 100px;">
  <div class="row">
    <div class="col-md-6 col-md-offset-3">
      <div>
        <!--bib-->
        <div class="row">
          <div class="col-md-4">
            <a id="tabs1" href="#tabone">tabone</a>
          </div>

          <div class="col-md-4">
            <a id="tabs2" href="#tabtwo">tabtwo</a>
          </div>

          <div id="tabs3" class="col-md-4">
            <a role ="tabpanel" href="#tabthree">tabthree</a>
          </div>
        </div>
    
        <!-- Nav tabs -->
        <ul class="nav nav-tabs" id="list-item" role="tablist">
          <li role="presentation" class="active"><a href="#tabone" aria-controls="tabone" role="tab" data-toggle="tab">tab1</a></li>
          <li role="presentation"><a href="#tabtwo" aria-controls="tabtwo" role="tab" data-toggle="tab">tab2</a></li>
          <li role="presentation"><a href="#tabthree" aria-controls="tabthree" role="tab" data-toggle="tab">tab3</a></li>
        </ul>

        <!-- Tab panes -->
        <div class="tab-content">
          <div role="tabpanel" class="tab-pane active" id="tabone">
            <h3>sample1</h3>
          </div>
          <div role="tabpanel" class="tab-pane" id="tabtwo">
            <h3>sample2</h3>
          </div>
          <div role="tabpanel" class="tab-pane" id="tabthree">
            <h3>sample3</h3>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

我希望b的每个元素乘以a,所以我可以得到一个3乘2的矩阵。

1 个答案:

答案 0 :(得分:2)

我只是使用outer

outer(a, b)
##      [,1] [,2]
## [1,]    4    5
## [2,]    8   10
## [3,]   12   15

您也可以使用sapply,如下所示:

sapply(a, "*", b)
##      [,1] [,2] [,3]
## [1,]    4    8   12
## [2,]    5   10   15