Help vectorizing calculation that accesses multiple entries in the input array and the previous element of the output array

I'm wondering if this can be vectorized. I'm accessing two entries in the input array along with the previous entry in the output array.
v and T are a vectors of floats.
[Edit: I left out some complexity and I'm not sure if the answer given can still be applied. I've updated the code below with the full expression.]
out = zeros(length(v), 1);
out(1) = 1;
for i = 2:length(v)
out(i) = out(i - 1) * (v(i) / v(i - 1)) * (T(i) / T(i - 1));
end

댓글 수: 2

Note: Avoid the square brackets around the counter in FOR loops. They can impede the JIT acceleration. Prefer:
for i = 2:length(v)

댓글을 달려면 로그인하십시오.

 채택된 답변

out=v./v(1)

댓글 수: 3

My original post left out some complexity as there's a 2nd vector involved too (this is a combined gas law calculation). Can a similar rearrangement still be applied here?
It still looks like it will just be,
Tv=T.*v;
out=TV./Tv(1)
You are correct, I see what's happening now. Thanks for your help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

제품

릴리스

R2019b

태그

질문:

2021년 3월 19일

댓글:

2021년 3월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by