Borrar filtros
Borrar filtros

error in a function, Error: In an assignment A(I) = B, the number of elements in B and I must be the same.

2 visualizaciones (últimos 30 días)
I have defined a piecewise function (below), where C1 (and hence x) is a vector. I don't understand why the error "In an assignment A(I) = B, the number of elements in B and I must be the same." is showing up for the boldfaced line.
function f = eta(C1)
N = 15;
x = N^(1/2).*C1;
f = zeros(size(x));
f(0<x<=0.5) = 1;
*f(0.5<x<=2/3) = -(x-1/2).^2+1;*
f(2/3<x<=5/6) = 384*x.^3-864*x.^2+(1919/3)*x-621/4;
f(5/6<x<=1) = (x-1).^2;
Thanks for the help!
  1 comentario
KSSV
KSSV el 11 de Ag. de 2017
f(0.5<x<=2/3) = -(x-1/2).^2+1;
The above is not valid.....because the condition 0.5<x<=2/3 gives some indices says m in number....where as right side is of size of C1..so you cannot save them into f..so the error.

Iniciar sesión para comentar.

Respuesta aceptada

Torsten
Torsten el 11 de Ag. de 2017
You see the difference ?
https://de.mathworks.com/matlabcentral/newsreader/view_thread/59214
Best wishes
Torsten.

Más respuestas (1)

KSSV
KSSV el 11 de Ag. de 2017
function f = eta(C1)
N = 15;
x = N^(1/2).*C1;
f = zeros(size(x));
f(x>0 & x<=0.5) = 1;
idx = 0.5<x<=2/3 ;
f(idx) = -(x(idx)-1/2).^2+1;
idx = 2/3<x<=5/6 ;
f(idx) = 384*x(idx).^3-864*x(idx).^2+(1919/3)*x(idx)-621/4;
idx = 5/6<x<=1 ;
f(idx) = (x(idx)-1).^2;

Categorías

Más información sobre Dependency Analysis 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