Equation in a single column matrix?
5 次查看(过去 30 天)
显示 更早的评论
I wanted to compute an equation in (n,1) matrix instead of (1,n) in a for loop.
Given
a =[27.7847; 31.1386,33.3644; 37.0654; 38.5043; 41.3808];
b = [13; 33; 55; 155; 245; 519];
When I use the following for loop, it gives me 6x6 matrix instead of 6x1.
for n = 1:6
c(n)= a(n)*0.4/(log(b(n/13));
end
Please modify the equation so that I can get answers in 6x1.
Thanks in advance.
0 个评论
采纳的回答
Jorg Woehl
2021-3-5
When I run your code (after fixing a typo when you refer to what I think should be b(n)/13), the result is a 1-by-6 array for c:
a =[27.7847; 31.1386; 33.3644; 37.0654; 38.5043; 41.3808];
b = [13; 33; 55; 155; 245; 519];
for n = 1:6
c(n)= a(n)*0.4/(log(b(n)/13));
end
c =
Inf 13.3705 9.2526 5.9820 5.2453 4.4894
To get c as a 6-by-1 vector instead, use c(n,1) inside the loop, or calculate the transpose c=c' after the loop is done.
Or even better, avoid the for loop altogether with the following vectorized assignment:
c = a.*0.4./(log(b./13))
This evaluates the expression one element at a time for a and b and constructs the vector c from the individual results.
更多回答(0 个)
另请参阅
类别
在 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!