Trying to make a piecewise function that isn't for graphing or using the piecewise function itself
    4 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Kylenino Espinas
 el 9 de Dic. de 2020
  
    
    
    
    
    Editada: James Tursa
      
      
 el 9 de Dic. de 2020
            function y = project2Tester(x)
  y = zeros(size(x));
  x1 == (x <= -1);
  y(x1) = log(abs(x));
  x2 == (-1 < x & x < 0);
  y(x2) = abs(x);
  x3 == (0 <= x & x < 1);
  y(x3) = x;
  x4 == (x >= 1);
  y(x4) = log(x);
  y(~(x1 | x2 | x3 | x4)) = 0;
end
Is my code. If I use = instead of == an error occrurs as well. If I use x1/2/3/4 instead of x into the y(x) = it still doesn't work. I'm kinda confused on where to go because I've tried everything. 
0 comentarios
Respuesta aceptada
  James Tursa
      
      
 el 9 de Dic. de 2020
        
      Editada: James Tursa
      
      
 el 9 de Dic. de 2020
  
      Use logical indexing on both sides of the assignment. E.g.,
function y = project2Tester(x)
  y = zeros(size(x));
  x1 = (x <= -1);
  y(x1) = log(abs(x(x1)));
  x2 = (-1 < x & x < 0);
  y(x2) = abs(x(x2));
  x3 = (0 <= x & x < 1);
  y(x3) = x(x3);
  x4 = (x >= 1);
  y(x4) = log(x(x4));
  y(~(x1 | x2 | x3 | x4)) = 0;  % not really needed since you initialized y to 0's
end
0 comentarios
Más respuestas (0)
Ver también
Categorías
				Más información sobre Delaunay Triangulation 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!

