Hello! I am trying to do a function that basically does this:
getInterpolationArrays(2,8)
x = [1, 3, 5, 7]
xp = [2, 4, 6, 8]
getInterpolationArrays(5,8)
x = [1, 6]
xp = [2, 3, 4, 5, 7, 8]
The array xp is like a "complement" of x. I writed the function below, but i get the following warning: Variable xp appears to change size on every loop iteration. I dont know how to solve it. Thank you for the response!
function [x, xp] = getInterpolationArrays(days, size)
x = 1:days:size-1;
n = length(x);
xp = [];
for i=1:size
if(ismember(i,x) == 0)
xp = [xp, i];
end
end
end

 Respuesta aceptada

Voss
Voss el 7 de Jun. de 2022
Editada: Voss el 7 de Jun. de 2022

1 voto

It's only a warning (not an error), so you don't have to solve it.
getInterpolationArrays is written such that xp is constructed element-by-element, so of course xp changes size as the loop iterates.
If you want to rewrite getInterpolationArrays to avoid that warning, you could do it like this:
[x,xp] = getInterpolationArrays(2,8)
x = 1×4
1 3 5 7
xp = 1×4
2 4 6 8
[x,xp] = getInterpolationArrays(5,8)
x = 1×2
1 6
xp = 1×6
2 3 4 5 7 8
function [x, xp] = getInterpolationArrays(days, size)
x = 1:days:size-1;
xp = 1:size;
xp = xp(~ismember(xp,x));
end

2 comentarios

Matías Bozo Pizarro
Matías Bozo Pizarro el 7 de Jun. de 2022
Thank you! I didn't find a way to make it so simple.
Voss
Voss el 7 de Jun. de 2022
You're welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 7 de Jun. de 2022

Comentada:

el 7 de Jun. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by