我的MATLAB代码中有两个函数,我希望一个函数的输出用作另一个函数的输入。我该如何实现呢?
medianfilter1.m
代码是:
function ans = medianfilter(angstanding)
[m,n] = size(angstanding);
ans = zeros(m,n);
x = angstanding(:,1);
y = angstanding(:,2);
z = angstanding(:,3);
svm = angstanding(:,4);
ans(:,1) = medfilt1(x);
ans(:,2) = medfilt1(y);
ans(:,3) = medfilt1(z);
ans(:,4) = medfilt1(svm);
第二个功能是:windowing.m
代码:
function [wx,wy,wz,wsvm] = windowing(currentdata)
% i = 1;
% obseen = 128;
[r,c] = size(currentdata);
% display(r);
% while obseen <= r
% i = i +1;
% obseen = obseen + 64;
%
%
% end;
% display(i);
residue = rem((r - 128),64);
rows = floor((r-128)/64);
rows = rows + 1;
wx = zeros(rows,128);
wy = zeros(rows,128);
wz = zeros(rows,128);
wsvm = zeros(rows,128);
vectorx = currentdata(:,1);
vectory = currentdata(:,2);
vectorz = currentdata(:,3);
vectorsvm = currentdata(:,4);
for i = 1:rows
x = (64 * (i-1)) + 1;
if i ~= rows
lastj = 128;
else
break ;
end;
for j = 1:lastj
wx(i,j) = vectorx(x);
wy(i,j) = vectory(x);
wz(i,j) = vectorz(x);
wsvm(i,j) = vectorsvm(x);
x = x + 1;
end;
end;
我希望将medianfilter.m
的结果输入windowing.m
。
以下是我尝试的内容:merge.m
function ans = medianfilter(rawdataset_train_acc)
[m,n] = size(rawdataset_train_acc);
ans = zeros(m,n);
x = rawdataset_train_acc(:,1);
y = rawdataset_train_acc(:,2);
z = rawdataset_train_acc(:,3);
svm =rawdataset_train_acc(:,4);
ans(:,1) = medfilt1(x);
ans(:,2) = medfilt1(y);
ans(:,3) = medfilt1(z);
ans(:,4) = medfilt1(svm);
function [wx,wy,wz,wsvm] = windowing(ans)
% i = 1;
% obseen = 128;
[r,c] = size(ans);
% display(r);
% while obseen <= r
% i = i +1;
% obseen = obseen + 64;
%
%
% end;
% display(i);
residue = rem((r - 128),64);
rows = floor((r-128)/64);
rows = rows + 1;
wx = zeros(rows,128);
wy = zeros(rows,128);
wz = zeros(rows,128);
wsvm = zeros(rows,128);
vectorx = ans(:,1);
vectory = ans(:,2);
vectorz = ans(:,3);
vectorsvm = ans(:,4);
for i = 1:rows
x = (64 * (i-1)) + 1;
if i ~= rows
lastj = 128;
else
break ;
end;
for j = 1:lastj
wx(i,j) = vectorx(x);
wy(i,j) = vectory(x);
wz(i,j) = vectorz(x);
wsvm(i,j) = vectorsvm(x);
x = x + 1;
end;
end;
我在上面试过但没有达到我想要的效果。我怎么能这样做?
答案 0 :(得分:1)
如果您有一个功能[output_a]=a(input)
和一个功能[output_b] =b(input)
你可以简单地将它们堆叠起来,因为@zizy archer已经建议了。它看起来像这样:
sample_input=3;
sample_ouput_var=a(b(sample_input));
该块的作用是将取样输入馈入b(),取b的输出并直接将其输入a。所以在伪代码中:
sample_input=3;
sample_outputb=b(sample_input):
sample_outputa=a(sample_output_b):
在你的特殊情况下,我会这样做
outa,outb, outc, outd =windowing(medianfilter(input))
如果你想合并两个功能区,我会得到第二个函数的输出和第一个函数的输入,所以在伪代码中这将是
function output1=examplefunc(input1):
output1=input1*input1
end
和第二个功能
function output2=examplefunc2(input2):
output2=input2+input2;
end
通过以下方式合并它们:
function outputmerged=merged_func(input1):
output1=input1*input1;
input2=output1;
output_merged=input2+input2;
end
在您的代码示例中,它看起来像这样:
function [wx,wy,wz,wsvm] = medianfilter_windowing_merge(rawdataset_train_acc)
%input from medianfilter output from windowing
[m,n] = size(angstanding);
ans = zeros(m,n);
x = angstanding(:,1);
y = angstanding(:,2);
z = angstanding(:,3);
svm = angstanding(:,4);
ans(:,1) = medfilt1(x);
ans(:,2) = medfilt1(y);
ans(:,3) = medfilt1(z);
ans(:,4) = medfilt1(svm);
%here happens the handover from one function to the other everything above is from the medianfilter.m everything below is from windowing
currentdata=ans
[r,c] = size(currentdata);
residue = rem((r - 128),64);
rows = floor((r-128)/64);
rows = rows + 1;
wx = zeros(rows,128);
wy = zeros(rows,128);
wz = zeros(rows,128);
wsvm = zeros(rows,128);
vectorx = currentdata(:,1);
vectory = currentdata(:,2);
vectorz = currentdata(:,3);
vectorsvm = currentdata(:,4);
for i = 1:rows
x = (64 * (i-1)) + 1;
if i ~= rows
lastj = 128;
else
break ;
end;
for j = 1:lastj
wx(i,j) = vectorx(x);
wy(i,j) = vectory(x);
wz(i,j) = vectorz(x);
wsvm(i,j) = vectorsvm(x);
x = x + 1;
end;
end;
答案 1 :(得分:1)
我不完全明白你在尝试什么。在我看来,你想要的确实是复合函数。
一旦你有了一个函数原型/定义,就必须调用它。
例如,如果你有计算立方体的函数和数字的平方(分别为cubeExample和squareExample),并且你想计算数字x
的6次幂:
result = squareExample(cubeExample(x))
function r = squareExample(a)
r = a*a;
end
function r = cubeExample(a)
r = a*a*a;
end
另请注意,函数定义的名称是静音,这意味着您的名称并不重要。
在这种情况下会像
[wx,wy,wz,wsvm] = windowing(medianfilter(angstanding))
根据您的想法,假设窗口的输入格式与medianfilter的输出相同。