How to vectorize this loop ?
显示 更早的评论
I am a fortran user and i am currently switching to matlab. I read that to be efficient, it is better to vectorize than using for loops.
How would you vectorize this loop ?
for i=init:fina
a(i,i+nf)=b(i)*c(i);
end
I tried this but it does not work as "a" is a matrix and "b" and "c" are two vectors ; how to include a vector which is not a row or a column in a matrix ?
i=init:fina;
a(i,i+nf)=b(i).*c(i);
And I have also the inverse problem...how to extract a vector from a matrix a(i,i+nf) to be computed with another one like this :
b(i)=a(i,i+nf).*c(i);
Last but not least, the vectorization in these cases will be more efficient than the for loop ?
Kind regards.
FP
1 个评论
Jos (10584)
2017-3-2
编辑:Andrei Bobrov
2017-3-2
- What are at the inputs (init, fina, nf, b, c)?
- Does a exist already? or is it created on the spot?
- Perhaps you can give a small example of the inputs and the expected output?
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 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!