Multiplying each value of a vector with the corresponding value in another vector?

17 ビュー (過去 30 日間)
I have 4 vectors E,A,alpha,temp (each 21x1)
I want to carry out the following equation:
theta = E*A*alpha*temp [-1 ;1]
and theta be a vector where the first value is: theta(1) = E(1)*A(1)*alpha(1)*temp(1) * [-1;1]
and theta(n) = E(n)*A(n)*alpha(n)*temp(n) * [-1;1] etc.
Do I use for loop, how would I got about this?
Thank you in advance.

採用された回答

Star Strider
Star Strider 2020 年 11 月 13 日
Use element-wise multiplication. If all the other vectors are column vectors, the [-1 1] vector must be a row vector:
theta = E.*A.*alpha.*temp*[-1 1]
See Array vs. Matrix Operations for an explanation.
  2 件のコメント
Finn Farnan
Finn Farnan 2020 年 11 月 13 日
This worked, Thank you!
Star Strider
Star Strider 2020 年 11 月 13 日
As always, my pleasure!

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

その他の回答 (2 件)

Matt J
Matt J 2020 年 11 月 13 日
theta = (E.*A.*alpha.*temp.*[-1 ,1]).'

Setsuna Yuuki.
Setsuna Yuuki. 2020 年 11 月 13 日
編集済み: Setsuna Yuuki. 2020 年 11 月 13 日
with loop for
for n = 1:length(E)
theta(:,n) = E(n)*A(n)*alpha(n)*temp(n) * [-1;1]
end
or can be:
theta(n) = E.*A.*alpha.*temp(n).*[-1;1]
  2 件のコメント
Finn Farnan
Finn Farnan 2020 年 11 月 13 日
Thank you for your response.
I have tried this solution and get "Unable to perform assignment because the left and right sides have a different number of elements."
Any ideas?
Setsuna Yuuki.
Setsuna Yuuki. 2020 年 11 月 13 日
編集済み: Setsuna Yuuki. 2020 年 11 月 13 日
My error in the code:
theta(n) = E.*A.*alpha.*temp(n).*[-1;1]
%is
theta= E.*A.*alpha.*temp.*[-1;1]
loop:
theta(:,n) = ...

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by