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.
  댓글 수: 1
Mingde
Mingde 2022년 7월 20일
thank you. I'm appreciate your answer.

댓글을 달려면 로그인하십시오.

추가 답변 (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