How do I make this array index into a function of two other array indices?

I have the following arrays: cp, cx, theta, L
cp is a 71x1 array, cx is a 70x1 array, theta is a 70x1 array, L is a 70x1 array
I want to iterate:
for i=1:70
cx(i)=-L/(2)*(cp(i+1)+cp(i))*sin(theta(i));
end
I keep getting this error:
Unable to perform assignment because the left and right sides have a different number of elements.
Error in example (line 40)
cx(i)=-L/(2)*(cp(i+1)+cp(i))*sin(theta(i));
How do I fix this error?

Respuestas (1)

Matt J
Matt J el 18 de Sept. de 2023
Editada: Matt J el 18 de Sept. de 2023
Perhaps you meant to have this:
for i=1:70
cx(i)=-L(i)/2*(cp(i+1)+cp(i))*sin(theta(i));
end

1 comentario

Matt J
Matt J el 18 de Sept. de 2023
Editada: Matt J el 18 de Sept. de 2023
If so, then BTW the one-line,loop-free way to do this is,
cx=-L.*movmean(cp,2,'End','discard').*sin(theta);

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 18 de Sept. de 2023

Editada:

el 18 de Sept. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by