Using a for loop to print function values

2 ビュー (過去 30 日間)
Petch Anuwutthinawin
Petch Anuwutthinawin 2021 年 6 月 15 日
編集済み: Scott MacKenzie 2021 年 6 月 15 日
I am trying to make a function that inputs Vs and outputs 0 if Vs<=0.6, and outputs VL-0.6 if Vs>0.6. I need this function to loop for a series of input Vs vector of values, and output a vector of values based on the function. The code I wrote currently keeps overriding the VL value and outputs only one scalar value. How do I make it output a vector of values?
V=0:Vs
for k = 1:length(V);
if Vs(k)<=0.6; %if this is true
VL(k)=0;% it prints a 0
else
VL(k)=Vs-0.6; % prints a Vs-0.6 value.
end
end

採用された回答

Scott MacKenzie
Scott MacKenzie 2021 年 6 月 15 日
編集済み: Scott MacKenzie 2021 年 6 月 15 日
Seems you are confusing Vs with V. And what is VL-0.6 in your question? Try this... (outputs a vector of values)
V = rand(1,10); % test data --> your inputed Vs
for k = 1:length(V)
if V(k) <= 0.6 % if this is true
VL(k) = 0; % it prints a 0
else
VL(k) = V(k) - 0.6; % prints a V-0.6 value
end
end
VL
Actually, it is not clear what you are trying to do, but the code above outputs a vector for VL -- your main objective, if I understand correctly.

その他の回答 (0 件)

カテゴリ

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