how to multiply one array of vector in another ?

조회 수: 1 (최근 30일)
Amin Gan
Amin Gan 2016년 2월 10일
댓글: Star Strider 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

채택된 답변

Star Strider
Star Strider 2016년 2월 10일
Use bsxfun:
A=[1 2 3];
B=[10 20 30];
equation = bsxfun(@plus, (A'+A'.^2), B)
equation =
12 22 32
16 26 36
22 32 42
  댓글 수: 2
Amin Gan
Amin Gan 2016년 2월 10일
Thank you for your reply.
If I had a more complex equation such as:
eqn=A+sqrt(A^2-B), can I use bsxfun ?
thank you
Star Strider
Star Strider 2016년 2월 10일
My pleasure.
Yes. You can ‘nest’ the bsxfun calls:
eqn = bsxfun(@plus, A', sqrt(bsxfun(@minus, A'.^2, B)));
or break them out into two or three lines to make them easier to read and (if necessary) troubleshoot:
eqn1 = bsxfun(@minus, A'.^2, B);
eqn2 = sqrt(eqn1);
eqn = bsxfun(@plus, A', eqn2)
Both produce:
eqn =
1 + 3i 1 + 4.3589i 1 + 5.3852i
2 + 2.4495i 2 + 4i 2 + 5.099i
3 + 1i 3 + 3.3166i 3 + 4.5826i
The results are complex, because you’re taking the square root of negative numbers.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by