Hello everone,
Here, line 4 and 5 is called 5525575680 times and they last 280 and 230 seconds in order. May I speed it up, how can I use it more efficient ?
1 function [utilization] = totalUtilization(myArray, periodNumber, count, LPP)
2 duration = 0;
3 for i = 1:count
4 duration = duration + myArray(periodNumber, i);
5 end
6 utilization = duration / LPP;
7 end
Thanks in advance.

댓글 수: 1

cglr
cglr 2019년 11월 16일
Because, I still can't get used to ability of MATLAB :) .
You are exactly true and no need to loop for sum or etc. I wrote the function taking into account to your comment:
utilization = sum(myArray(periodNumber,:)) / LPP;
Thank you so much.

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

 채택된 답변

Daniel M
Daniel M 2019년 11월 16일
편집: Daniel M 2019년 11월 16일

1 개 추천

Why do you need a loop to compute a sum? Just replace the entire function with
utilization = sum(myArray(periodNumber, 1:count)) / LPP;
And if count is equal to the number of columns of myArray, replace "1:count" with just ":" to avoid making an extra temporary index.
If periodNumber happens to be a vector, then include the number 2 as the dimension input into sum.

댓글 수: 4

cglr
cglr 2019년 11월 16일
Also, I would like to ask one more question.
for i=1:period
utilization(i,1) = myArray(table, i, LPP) * 100;
end
What do you offer for this loop ?
Daniel M
Daniel M 2019년 11월 16일
편집: Daniel M 2019년 11월 16일
What is table in this code? Is it an empty table using the table() function? https://www.mathworks.com/help/matlab/ref/table.html
Is it a table named table? Because you're not accessing any of its properties.
If it is a regular array then no need for a loop, just replace "i" with ":"
cglr
cglr 2019년 11월 17일
actually table is a regular array with the size (period,telCount). And I can ask the question in a better way as seen below:
x = zeros(period,1);
for i=1:period
x(i,1) = sum(table(i,1:telCount));
end
Here what I want is, sum all column for each row and write result to x array.
Daniel M
Daniel M 2019년 11월 17일
You really enjoy writing loops! But you don't need to. Look up the documentation for the sum function. It shows you how to do just that.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

질문:

2019년 11월 16일

댓글:

2019년 11월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by