How can i make this code block a function?

1 回表示 (過去 30 日間)
Enes Ozdogan
Enes Ozdogan 2021 年 11 月 29 日
編集済み: Jan 2021 年 11 月 29 日
z=[];
for i=1:length(x)
g=h.*x(i);
z=[z;g];
end
[r c]=size(z);
k=r+c;
t=2;
y=[];
cd=0;
while(t<=k)
for i=1:r
for j=1:c
if((i+j)==t)
cd=cd+z(i,j);
end
end
end
t=t+1;
y=[y cd];
cd=0;
end
what variables should i put into my parameters I tried something like below
function Y=my_conv(x,h)
-----
end
didnt work out as expected
  2 件のコメント
Jan
Jan 2021 年 11 月 29 日
編集済み: Jan 2021 年 11 月 29 日
Please mention, what "didn't work as expected" means. Of course this would be the way to go. If you have any troubles with this, explain us, which problem you have. It is much easier to solve a problem than to guess, what the problem is.
The only problem I see is the uppercase Y, while you use the lowercase y in the code.
Enes Ozdogan
Enes Ozdogan 2021 年 11 月 29 日
Unrecognized function or variable i get this error when i run the program sorry for not mentioining it.

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

回答 (1 件)

Jan
Jan 2021 年 11 月 29 日
A simplified version of your code:
function y = my_conv(x, h)
z = x(:) .* h;
[r, c] = size(z);
y = [];
for t = 2:r+c
cd = 0;
for i = 1:r
for j = 1:c
if i+j == t
cd = cd + z(i,j);
end
end
end
y = [y, cd];
end
end
It works correctly.

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by