Avoid for loop which has an if-statement in it

I have a for-loop process, in which I have to check sth, so I have a if statement. How can I avoid for-loop to have a faster code:
for i=1:n
score = find sth in a cell array
if ~isempty(score)
M(i)=score;
do sth;
end
end
in the above code, I try to find sth in a cell array and if I find it then the score is not empty, and then fill an array M and do some other things.
Thank you

6 件のコメント

James Tursa
James Tursa 2016 年 3 月 30 日
This will depend on whether "find sth in a cell array" can be vectorized and what "do sth" means. Can you elaborate on exactly what is going on in these lines?
Shima Asaadi
Shima Asaadi 2016 年 3 月 30 日
編集済み: Shima Asaadi 2016 年 3 月 30 日
The complete code is like the following. "features_score" is a cell array with 2 columns. the first column includes different strings and the second column is a double value(score) of each string. "str" is a special string that I look for its score in "features_score". Then, if I find the "str" in this "features_score", I will put the score of it in M(i,j); each element of matrix M is associated to a special "str". So I have to go through the matrix and do the following for each associated "str" of the matrix elements.
[m,n]=size(M);
for i=1:m
for j=1:n
str = "independent of this code"
score = features_score(strcmp(str,features_score(:,1)),2);
if ~isempty(score)
M(i,j) = score;
end
end
end
I would like to know if I can avoid this time-consuming for-loop
Thanks
Matthew Eicholtz
Matthew Eicholtz 2016 年 3 月 30 日
Can you provide values for m and n? I'm just curious so I can get a better sense of how long the for-loop approach would take.
Shima Asaadi
Shima Asaadi 2016 年 3 月 30 日
well, m and n are ~20000 !
Matthew Eicholtz
Matthew Eicholtz 2016 年 3 月 30 日
Also, how are i and j involved in the following two lines?
str = "independent of this code"
score = features_score(strcmp(str,features_score(:,1)),2);
Shima Asaadi
Shima Asaadi 2016 年 3 月 30 日
"str" is dependent of i and j. I mean that, for each element of the matrix, I have to set a value of an associated "str". But, I should find the value of "str" in features_score, and if it is not in the features_score, the element of the matrix is zero.

サインインしてコメントする。

回答 (2 件)

Guillaume
Guillaume 2016 年 3 月 30 日
編集済み: Guillaume 2016 年 3 月 30 日

1 投票

Well, str can't be independent of the loop otherwise your code just repeat the exact same thing m x n times.
Assuming str is an m x n cell aray of strings, you can use ismember to replace the loops:
%demo data;
str = {'a', 'b', 'cc'; 'ff', 'notpresent', 'ddd'};
features_score = {'a', 100; 'b', 200; 'cc', 300; 'ddd', 400; 'e', 500; 'ff', 600};
[ispresent, row] = ismember(str, features_score(:, 1));
M = zeros(size(str));
M(ispresent) = [features_score{row(ispresent), 2}]

1 件のコメント

Shima Asaadi
Shima Asaadi 2016 年 3 月 30 日
編集済み: Shima Asaadi 2016 年 3 月 31 日
Thank you for reply.
sorry, I mean that it is not relevant to the code I provided, because I do some calculations, but yes, it is dependent of the variable i and j.

サインインしてコメントする。

Jan
Jan 2016 年 3 月 31 日

0 投票

Avoiding FOR loops is often overestimated. Loops are not a general problem in Matlab. They can be accelerated as in all computer languages by avoiding repeated calculations inside the loops. If this matters in your case cannot be seen as long as you post some abstract pseudo-code only. This simply hides the interesting and important details.
As usual it is not worth to accelerate the loops, if they do not consume the main part of the computing time. So use the profile command to identify the bottleneck at first and care about this only. And acceleration of 100% of a piece of code, which needs only 2% of the total processing time, reduces the run time by only 1%.
I suggest to do both: Use the profile at first, and then post the relevant code in the forum - as real code with useful test data, not as pseudo code.

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2016 年 3 月 30 日

回答済み:

Jan
2016 年 3 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by