create function in loop

2 ビュー (過去 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 Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by