create function in loop

1 次查看(过去 30 天)
Mingde
Mingde 2022-7-20
评论: Mingde 2022-7-20
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

采纳的回答

Jan
Jan 2022-7-20
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by