Rounding numbers below 0.7 to 0.7

5 ビュー (過去 30 日間)
Ashik Chowdhury
Ashik Chowdhury 2020 年 2 月 16 日
コメント済み: Stephen23 2020 年 2 月 16 日
I want to round all the numbers between 0 and 0.7 to 0.7.
for i = 1:numel(efficiency_final)
if(efficiency_final)<0.7
effciency_final_array = 0.7;
else
efficiency_final_array = efficiency_final;
end
end
For some reason this code never executes the if statement in the for loop. Only goes to the else statement and basically gives me the same values in efficiency_final in the efficiency_final_array.
Is there a better way of doing this?
  1 件のコメント
Stephen23
Stephen23 2020 年 2 月 16 日
"Is there a better way of doing this?"
Use the max method shown in Walter Roberson's answer:

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

採用された回答

Bhaskar R
Bhaskar R 2020 年 2 月 16 日
efficiency_final(efficiency_final<0.7) = 0.7
  1 件のコメント
Ashik Chowdhury
Ashik Chowdhury 2020 年 2 月 16 日
Thank you that worked :)

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 2 月 16 日
Recommended:
efficiency_final_array = max(efficiency_final, 0.7);
For some reason this code never executes the if statement in the for loop
for i = 1:numel(efficiency_final)
if(efficiency_final(i))<0.7 %notice the indexing
effciency_final_array(i) = 0.7;
else
efficiency_final_array(i) = efficiency_final(i);
end
end
  2 件のコメント
Ashik Chowdhury
Ashik Chowdhury 2020 年 2 月 16 日
Your recommended method works. Thank you :)
Can you tell me whats wrong with the indexing so that i can understand why it didnt work.
Steven Lord
Steven Lord 2020 年 2 月 16 日
The if statement's condition is satisfied only if it is nonempty and all the elements are nonzero.
Your original if statement's condition would only be satisfied if all the elements of efficiency_final were less than 0.7.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by