how to multiply one array of vector in another ?

1 回表示 (過去 30 日間)
Amin Gan
Amin Gan 2016 年 2 月 10 日
コメント済み: Walter Roberson 2016 年 2 月 10 日
There are two same size vectors,A and B (1*2000 in size).
for each value of A must be used in equation below for all B values:
equation=A+A^2+B
for example:
A=[1 2 3]
B=[10 20 30]
I want following results:
1+1^2+10 , 1+1^2+20, 1+1^2+30
2+2^2+10 , 2+2^2+20, 2+2^2+30
3+3^2+10 , 3+3^2+20, 3+3^2+30
But, A and B both are 2000 in size, I expect to get 2000*2000 matrix for eqn.
can you please advise?
Thnks

採用された回答

Walter Roberson
Walter Roberson 2016 年 2 月 10 日
bsxfun(@(a,b) a + a.^2 + b, A(:), B)
  2 件のコメント
Amin Gan
Amin Gan 2016 年 2 月 10 日
Thanks for your reply. If I had a more complex equation such as:
eqn=A+sqrt(A^2-B), can I use bsxfun ?
Walter Roberson
Walter Roberson 2016 年 2 月 10 日
Yes. Just be sure to use .^ instead of ^ . Also be sure that your B is a row vector
bsxfun(@(a,b) a + sqrt(a.^2 - b), A(:), B)
If B is not a row vector then
bsxfun(@(a,b) a + sqrt(a.^2 - b), A(:), B(:).')

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by