Info

この質問は閉じられています。 編集または回答するには再度開いてください。

improve performance of if statment

1 回表示 (過去 30 日間)
bkshn
bkshn 2015 年 7 月 2 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hello
I have bellow statement
if ((fullPath(i-1)-1) > y)
col1=y ;
else
col1=(fullPath(i-1)-1);
end
if fullPath(i-1) > y
col2=y ;
else
col2=fullPath(i-1);
end
if (fullPath(i-1)+1) > y
col3=y ;
else
col3=(fullPath(i-1)+1) ;
end
How can I improve performance of these "if"?
as I know in C# there is if statement like bellow
condition ? statement1:statement2 (that if condition is true statement1 is going to do and if condition is false statement2 is going to do)
is there any command like this in matlab?

回答 (1 件)

Guillaume
Guillaume 2015 年 7 月 2 日
編集済み: Guillaume 2015 年 7 月 2 日
C languages' ?: is called the ternary conditional operator. There is no such thing in matlab unfortunately.
In your case, you could replace the if ... else, by:
col1 = min(y, fullPath(i-1)-1);
%same with col2, col3
Note: fullPath is a very odd variable name for something containing a number. I would have thought it'd be a string.
  1 件のコメント
Mark Matusevich
Mark Matusevich 2015 年 7 月 2 日
Minor optimisation, do all 3 in one command:
col = min(y, fullPath(i-1) + (-1:1));
then col is a vector with elements [col1 col2 col3].

この質問は閉じられています。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by