How can I turn a data vector into a matrix of rows with the same vector?

1 回表示 (過去 30 日間)
Alfonso Rodriguez
Alfonso Rodriguez 2016 年 11 月 7 日
コメント済み: Alfonso Rodriguez 2016 年 11 月 7 日
I have a matrix A which has different kind of data points. I also have a vector of constant values that need to be substract from each row from the matrix A. The problem is that a Matrix and a vector have different dimensions and can´t do arithmetic like that. I will provide an example:
A = [1,2;3,4] b = [5,6]
I want to turn vector b into a matrix having (n) row values being the same as the first row values. This way I will have a matrix: B = [5,6;5,6] I hope this makes sense. This is needed for a math formula in geology.

採用された回答

James Tursa
James Tursa 2016 年 11 月 7 日
編集済み: James Tursa 2016 年 11 月 7 日
If you just need to do the subraction:
result = bsxfun(@minus,A,b);
If you need your B for some other reason also, then:
B = repmat(b,size(A,1),1);
result = A - B;
  2 件のコメント
Andrei Bobrov
Andrei Bobrov 2016 年 11 月 7 日
or just for R2016b:
A - b
Alfonso Rodriguez
Alfonso Rodriguez 2016 年 11 月 7 日
Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by