create function in loop
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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 Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!