フィルターのクリア

user defined function including a for loop taking vector input giving vector output

2 ビュー (過去 30 日間)
Hello,
myfunc1 takes in a vector and gives out a vector as expected.
but myfunc2, which has a for loop, takes in a vector and gives out a scalar value of 1.
can you please help me to fix myfunc2 so that it gives out a vector, which is a vector of :
(summation from 1 to 1, summation from 1 to 2, summation from 1 to 3, ..., summation from 1 to 10)
thank you //:-)
---------
clear all
clc
x=[1:10];
%myfunc2(x)
myfunc1(x)
function [y2]=myfunc2(p)
k=0;
for t=1:p
k=k+t;
end
y2=k;
%y2=p+1;
end
function [y1]=myfunc1(p)
y1=p+1;
end

採用された回答

Austin M. Weber
Austin M. Weber 2024 年 2 月 11 日
Is this what you are trying to do?
x = 1:10;
result = myfunc2(x)
result = 1×10
1 3 6 10 15 21 28 36 45 55
function [y2]=myfunc2(p)
k=0;
y2 = [];
for t = 1:length(p)
k = k + t;
y2 = [y2 k];
end
end
  1 件のコメント
Huhnkie
Huhnkie 2024 年 2 月 11 日
thank You, it worked //:-D happy valentines day and God bless You //:-)

サインインしてコメントする。

その他の回答 (1 件)

Torsten
Torsten 2024 年 2 月 11 日
編集済み: Torsten 2024 年 2 月 11 日
p = 10;
result = myfunc2(p)
result = 1×10
1 2 4 7 11 16 22 29 37 46
function [y2]=myfunc2(p)
y2 = zeros(1,p);
y2(1) = 1;
for t=1:p-1
y2(t+1)=y2(t)+t;
end
end
  1 件のコメント
Huhnkie
Huhnkie 2024 年 2 月 11 日
thank You, it worked //:-D happy valentines day and God bless You //:-)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by