matlab 自定义的函数 不能使用外面的矩阵吗

这个程序不是我写的
现在要求把矩阵table拉到函数外面(因为table在外面要通过某个程序得到的),里面不直接定义table,怎么弄啊?求大神指教!
function x=jdcdec(y)
%clear;clc
%y=[0 1 1 0 1];
% table specified
table=[2 0 0 0 0 0 0 0 0 0
3 0 1 0 0 0 0 0 0 0
3 0 1 1 0 0 0 0 0 0
3 1 0 0 0 0 0 0 0 0
3 1 0 1 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0
4 1 1 1 0 0 0 0 0 0
5 1 1 1 1 0 0 0 0 0
6 1 1 1 1 1 0 0 0 0
7 1 1 1 1 1 1 0 0 0
8 1 1 1 1 1 1 1 0 0
9 1 1 1 1 1 1 1 1 0];
N=length(y);
[p,dm1]=size(table);
x=[];
x1=[];
i=1; d=2;tmp=ones(p,1);
while length(x)<64,
% match y(i) to that of the d-th bit in the table
tmp=tmp.*[table(:,d)==y(i)]; % tmp is a vector of 0 and 1 with 1 indicate a match
if sum(tmp)==1, % narrow down to one symbol, find it
d=2; % reset pointer to columns of table.
kkt=0;
for kk=1:length(tmp)
if(tmp(kk)==1),kkt=kk;end
end
cat=kkt-1;
tmp=ones(p,1);
if cat==11,i=i+1;end % Because the comparison ends in last but one column,but still a 0 is left
x1=y(i+1:i+cat);
% Check Range
if(cat ~=0)
x2=bin2int(x1);
if(x2>=2^(cat-1) & x2<2^cat)
x2=x2;
else
x1=ones(1,cat)-x1;
x2=-1*bin2int(x1);
end
else
x2=0;
end
% Update decoded vector
x=[x x2];
i=i+cat;
else
d=d+1;
end
i=i+1;
end
x=filter(1,[1 -1],x); % Inverse of DPCM

1、通过参数传递,在函数中增加一个变量,table
2、将table设置成全局变量,声明一下就可以了
3、在table生成的程序末位加上save ***.mat,然后再需要调用table的程序开头加入load ***.mat 当然可以指定载入table一个变量的,在.mat 后面加入变量table
这三种方法都可以的。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-04-01
通过参数传递,将你的函数增加一个输入变量X 用X传递矩阵table本回答被提问者采纳
相似回答