how can I store this data in an array using loop?
1 次查看(过去 30 天)
显示 更早的评论
Hello. I am trying to run the below code. but its not working. I want to run the loop 5 times and calculate the value of x for 5 temperatures and store in a matrix.
please help. thanks
a=1;
v=2;
p=1e-5;
vac=zeros(1,5);
T=[77,295,600,750,1234];
for i=1:length(T)
x=exp(a)*exp(-v/(p*T));
vac(i)=x;
end
0 个评论
采纳的回答
更多回答(1 个)
Guillaume
2018-1-18
You forgot to mention what it is not working mean: You get error using / Matrix dimensions must agree. That's because you forgot to index T so you're dividing a scalar by a vector which is not a valid matrix operation, so one way to fix your problem:
x = exp(a)* exp(-v/(p*T(i)));
However, you don't need a loop at all, you could use vectorised operations. For that you need to use ./ instead of /:
a = 1; v = 2; p = 1e-5; %constants
T = [77, 295, 600, 750, 1234];
vac = exp(a)*exp(-v./(p*T)) %no loop needed
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!