Create function for matlab code

Hi everyone,
I have created an algorithm code as below, but I cannot specify it as a function, can you help please?
N = 138;
j=1;
for i = 1:2:N
sira(1,i) = int32(j);
sira(1,i+1) = int32(j);
j = j+1;
end
newM=birincifaz.*(1:size(birincifaz,1))';
newM(:,1)=[];
for k=1:size(newM,2)
r=unique(newM(:,k));
r=r(r~=0);
B(:,k)=r(randi(length(r),20,1));
end
yeni=ikincifaz.*(1:size(ikincifaz,1))';
yeni(:,1)=[];
for e=1:size(yeni,2)
m=unique(yeni(:,e));
m=m(m~=0);
H(:,e)=m(randi(length(m),20,1));
end
out = reshape(vertcat(B,H), 20, []);
D = [sira ; out]
new_idx = randperm(size(D,2)/2)*2-[1; 0];
rassall= D(:,new_idx(:))
Thanks in advance

回答 (2 件)

Star Strider
Star Strider 2022 年 3 月 22 日

0 投票

See the documentation section on Function Basics.

2 件のコメント

Yonca DOGU
Yonca DOGU 2022 年 3 月 22 日
yes i checked but i am getting error.
Steven Lord
Steven Lord 2022 年 3 月 22 日
Please show us the code you've written to try to convert your script into a function as well as how you're trying to call that function and the full and exact text of the error and/or warning messages you receive when you try running it (all the text displayed in red and/or orange in the Command Window.)

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

Image Analyst
Image Analyst 2022 年 3 月 22 日

0 投票

Why are you and so many others posting the same code with
newM=birincifaz.*(1:size(birincifaz,1))';
in it? Was this a class assignment that you're suppose to fix by yourselves?

7 件のコメント

Yonca DOGU
Yonca DOGU 2022 年 3 月 22 日
No this isn't an assignment my friends acted unannounced, we are working on a project, we do not know matlab well, so we need your help to complete it.
Torsten
Torsten 2022 年 3 月 22 日
What variables are input to the function you want to create and what variables do you want it to output ?
Image Analyst
Image Analyst 2022 年 3 月 22 日
@Yonca DOGU are you saying that of all the other answers that your friends got, none of them solved the problem?
Yonca DOGU
Yonca DOGU 2022 年 3 月 22 日
@Image Analyst We couldn't find a solution for this question..
Yonca DOGU
Yonca DOGU 2022 年 3 月 22 日
@Torsten our inputs are "birincifaz,ikincifaz" and our output is rassall.
Torsten
Torsten 2022 年 3 月 22 日
function [rassall] = f(birincifaz,ikincifaz)
N = 138;
j=1;
for i = 1:2:N
sira(1,i) = int32(j);
sira(1,i+1) = int32(j);
j = j+1;
end
newM=birincifaz.*(1:size(birincifaz,1))';
newM(:,1)=[];
for k=1:size(newM,2)
r=unique(newM(:,k));
r=r(r~=0);
B(:,k)=r(randi(length(r),20,1));
end
yeni=ikincifaz.*(1:size(ikincifaz,1))';
yeni(:,1)=[];
for e=1:size(yeni,2)
m=unique(yeni(:,e));
m=m(m~=0);
H(:,e)=m(randi(length(m),20,1));
end
out = reshape(vertcat(B,H), 20, []);
D = [sira ; out]
new_idx = randperm(size(D,2)/2)*2-[1; 0];
rassall= D(:,new_idx(:))
end
Yonca DOGU
Yonca DOGU 2022 年 3 月 22 日
@Torsten Thank u for your help

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

質問済み:

2022 年 3 月 22 日

コメント済み:

2022 年 3 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by