フィルターのクリア

Returning values based on range of variable values

3 ビュー (過去 30 日間)
Kevin Bodell
Kevin Bodell 2021 年 4 月 9 日
回答済み: Chendi Lin 2021 年 4 月 9 日
I'd like my code to return 1 of 3 values for a given range of values within another variable, but cannot seem to get it to work. I've tried both if statements within a for loop as well as while statements, neither of which are returning my desired result. While neither code returns any sort of error, the original variable which should hold 1 of 3 values is never defined in my workspace. Below are both of my attempts. Any help is greatly appreciated.
%For/If:
x = 0:0.01:L;
nx = numel(x);
for n = 1:nx-1
if x<=0.2
U(n,0) = 1;
elseif x == 0.2
U(n,0) = 0.5;
else x>=0.2
U(n,0) = 0;
end
end
%While:
L=1;
x = 0:0.01:L;
while x<=0.2
U(x,0) = 1
end
while x == 0.2
U(x,0) = 0
end
while x>=0.2
U(x,0) = 0
end

回答 (1 件)

Chendi Lin
Chendi Lin 2021 年 4 月 9 日
Hi Kevin,
The index of MATLAB starts from 1. Have you tried U(x,1)?
Best,
CD

カテゴリ

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