# How to perform a calculation only when a input value is positive

1 ビュー (過去 30 日間)
Kevin Burg 2021 年 5 月 31 日
コメント済み: Kevin Burg 2021 年 5 月 31 日
I have a input file that has 1x367 inputs. I only want to calculate the following equation when the input value is positive. I thought this would be done through a for loop but that appears to not not be valid. How can I perform this calculation such that only a positive value is used? I thought a for loop would be the correct choice and also tried an if statement. Is there a better way to do this?
for chi_e > 0.0
j_l = ((1-chi_e)*m_dot)/(rho_L*((pi*D^2)/4));
end

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

### 採用された回答

Abhinav Gupta 2021 年 5 月 31 日

Let your input variable be a of size = 1x367
Let b be the variable which consist of all positive elements of a.
b = a( a>= 0 );
for i = 1:1:length(b)
j_l = ((1-b(i))*m_dot)/(rho_L*((pi*D^2)/4));
disp(j_l);
end
Or simply,
b = a( a>= 0 );
j_l = ((1-b)*m_dot)/(rho_L*((pi*D^2)/4));
##### 1 件のコメント表示非表示 なし
Kevin Burg 2021 年 5 月 31 日
This is exactly what I was looking for, thanks!

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

### その他の回答 (1 件)

Scott MacKenzie 2021 年 5 月 31 日
It is not clear what you mean by "input value", but assuming you do know, then...
for chi_e > 0.0
if input_value > 0
j_l = ((1-chi_e)*m_dot)/(rho_L*((pi*D^2)/4));
end
end
##### 1 件のコメント表示非表示 なし
Kevin Burg 2021 年 5 月 31 日
Sorry, I have a pre-calculated matrix of positive and negative values for chi_e. I only want to use positive values of chi_e in the j_l formula.

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

### カテゴリ

Find more on Loops and Conditional Statements in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by