How to avoid using 'for' loop

'u' and 'v' are 401*401 matrices and 'R' is a 401*1 matrix. I have a function which gets 'u' and 'v' vectors and one value of 'R' per iteration and calculates the value of the function. Then after that, I have to sum all these values to get the final result. I wish to achieve my goal without using 'for' loop. Is it possible?
Example:
% Initialize 'u', 'v', and 'R' matrices
u = ...; % 401x401 matrix
v = ...; % 401x401 matrix
R = ...; % 401x1 matrix
functionValues =zeros(401,401);
% Calculate the values of the function for each element
for i=1:numel(R)،
functionValues = functionValues + calculateValue(u, v, R(i)); % 401x401 matrix
end
In fact I wish to avoid using 'for' loop to increase running speed.

댓글 수: 1

moh mor
moh mor 2023년 10월 22일
Note: 'functionValues' has the same dimension as 'u' and 'v'. In fact I wish to sum up values of 'functionValues' for different 'R' at each position of 'functionValues'.

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

답변 (2개)

Image Analyst
Image Analyst 2023년 10월 22일

0 개 추천

Just modify youir calculateValue() so that it takes multiple values of R (can handle R being a vector) and returns a vector instead of a single scalar number. Then just call the function -- no for loop needed.

댓글 수: 2

moh mor
moh mor 2023년 10월 22일
Sorry, I think sth is misunderstood. 'functionValues' has the same dimension as 'u' and 'v'. In fact I wish to sum up values of 'functionValues' for different 'R' at each position.
Then why not
theSum = 0;
for k = 1 : numel(R)،
theSum = theSum + calculateValue(u, v, R(k)); % 401x401 matrix
end
And I agree with @Dyuman Joshi with only a few iterations (401*401) you'd most likely not notice any difference at all. It might be a different story if you had hundreds of millions of elements.

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

Dyuman Joshi
Dyuman Joshi 2023년 10월 22일

0 개 추천

Vectorize the function calculateValue() and use sum for calculateValue(u, v, R).
However, this assumes that calculateValue() can be vectorized.
And if it can be, I doubt if you will see any noticeable speed increase by using vectorization instead of using a for loop.

댓글 수: 5

moh mor
moh mor 2023년 10월 22일

Sorry, I think sth is misunderstood. 'functionValues' has the same dimension as 'u' and 'v'. In fact I wish to sum up values of 'functionValues' for different 'R' at each position.

moh mor
moh mor 2023년 10월 22일
I have heard that deploying 'for' loop decrease the speed by a huge amount.
Dyuman Joshi
Dyuman Joshi 2023년 10월 22일
"I have heard that deploying 'for' loop decrease the speed by a huge amount."
No, that is not generally true. If utilized properly, for loops can be faster.
However, Vectorization is faster because MATLAB is optimized for it. But that does not mean for loop are very slow.
On the other hand, if someone writes an inefficient for loop, then it might be slower, but that is because the way the code was written, not due to the nature of the for loop.
"'Sorry, I think sth is misunderstood. functionValues' has the same dimension as 'u' and 'v'. In fact I wish to sum up values of 'functionValues' for different 'R' at each position."
I understood your query perfectly and yes, I know that.
Using a for loop, the sum has to be updated in each iteration, whereas in the case of vectorization, the values are calculated together.
Check out this link to know more about vectorization - Vectorization.
Did you try to vectorize the function calculateValue()?
moh mor
moh mor 2023년 10월 22일
편집: Walter Roberson 2023년 10월 25일
As much as I could, I've Vectorized my problem.
Dyuman Joshi
Dyuman Joshi 2023년 10월 22일
편집: Dyuman Joshi 2023년 10월 25일
@moh mor, is there still a problem?
Does it work properly? And did you check the speed of the two methods for comparison?

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

제품

릴리스

R2018b

질문:

2023년 10월 22일

편집:

2023년 10월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by