Plotting Binary step function

9 visualizaciones (últimos 30 días)
Linu Pinto
Linu Pinto el 22 de En. de 2020
Respondida: Robert U el 22 de En. de 2020
How to plot the following step function function by avoiding vertical lines between discontinuities
x=[-10,10]
y=sin(x)
z=1 if y<0
z=0 if y>=0
plot(x,z);

Respuestas (1)

Robert U
Robert U el 22 de En. de 2020
Hi Linu Pinto,
You could plot each section separately.
x = -10:0.1:10;
y = sin(x);
z(y<0) = 1;
z(y>=0) = 0;
nZNew =[0 find(diff(z) ~= 0) length(z)];
zNew = arrayfun(@(nStart,nEnd) z(nStart+1:nEnd),nZNew(1:end-1),nZNew(2:end),'UniformOutput',false);
xNew = arrayfun(@(nStart,nEnd) x(nStart+1:nEnd),nZNew(1:end-1),nZNew(2:end),'UniformOutput',false);
fh = figure;
ah = axes(fh);
hold(ah,'on');
cellfun(@(xIn,zIn) plot(xIn,zIn,'-blue'),xNew,zNew)
Kind regards,
Robert

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by