Multiplying each value of a vector with the corresponding value in another vector?
20 次查看(过去 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.
0 个评论
采纳的回答
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]
更多回答(2 个)
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 个评论
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 Exchange 中查找有关 Optimization Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!