Borrar filtros
Borrar filtros

Returning values based on range of variable values

3 visualizaciones (últimos 30 días)
Kevin Bodell
Kevin Bodell el 9 de Abr. de 2021
Respondida: Chendi Lin el 9 de Abr. de 2021
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

Respuestas (1)

Chendi Lin
Chendi Lin el 9 de Abr. de 2021
Hi Kevin,
The index of MATLAB starts from 1. Have you tried U(x,1)?
Best,
CD

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by