create function in loop

1 visualización (últimos 30 días)
Mingde
Mingde el 20 de Jul. de 2022
Comentada: Mingde el 20 de Jul. de 2022
Hello
Can you help me to create function for this script?
%%Center of mass at 30second
F_sum4 = 0;
start=1;
stop=810;
for n = start:stop
F_sum4 = F_sum4 + F(:,:,n)
CY_sum4 = mean(cy_sum(start:stop),'omitnan')
CX_sum4 = mean(cx_sum(start:stop),'omitnan')
% if rem(m,27)==0
end
imshow(F_sum4,[]); hold on
plot(CX_sum4,CY_sum4,'*','markersize',30)
hold off
F_sum5 = 0;
start=810;
stop=1620;
for a = start:stop
F_sum5 = F_sum5 + F(:,:,a)
CY_sum5 = mean(cy_sum(start:stop),'omitnan')
CX_sum5 = mean(cx_sum(start:stop),'omitnan')
% if rem(m,27)==0
end
imshow(F_sum5,[]); hold on
plot(CX_sum5,CY_sum5,'*','markersize',30)
F_sumA= F_sum4+F_sum5
imshow(F_sumA,[]); hold on
plot(CX_sum4,CY_sum4,'*','markersize',30)
plot(CX_sum5,CY_sum5,'*','markersize',30)
%plot(CX_sum4,CY_sum4,'*','markersize',30)
% plot(CX_sum,CY_sum,'*','markersize',30)
hold off

Respuesta aceptada

Jan
Jan el 20 de Jul. de 2022
Simplify
F_sum4 = 0;
start=1;
stop=810;
for n = start:stop
F_sum4 = F_sum4 + F(:,:,n)
CY_sum4 = mean(cy_sum(start:stop),'omitnan')
CX_sum4 = mean(cx_sum(start:stop),'omitnan')
end
to
start = 1;
stop = 810;
F_sum4 = sum(F(:,:,start:stop), 3);
CY_sum4 = mean(cy_sum(start:stop),'omitnan')
CX_sum4 = mean(cx_sum(start:stop),'omitnan')
No loop needed. The 2nd loop can be avoided also.
To get a function, simply add the line "function YourFuncName(F, cx_sum, cy_sum)" as first line.
  1 comentario
Mingde
Mingde el 20 de Jul. de 2022
thank you. I'm appreciate your answer.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements 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