如何矢量化编程


% 定义含有三个样本的符号运算

% d : nsample * nlabel
% lnd : nsample * nlabel
nlabel = 5;
nsample = 3;
feat_len = 4;

% lambda : nlabel nlabel
for i = 1:nlabel
    for j = 1:nlabel
        lambda(i,j) = sym(['lambda' num2str(i) num2str(j)],'real');
    end
end
% d: nsample * nlabel
for i = 1:nsample
    for j = 1:nlabel
        d(i,j) = sym(['d' num2str(i) num2str(j)],'real');
    end
end
% lnd : nsample * nlabel
for i = 1:nsample
    for j = 1:nlabel
        lnd(i,j) = sym(['lnd' num2str(i) num2str(j)],'real');
    end
end

for i = 1:nsample
    for j = 1:nlabel
        lnp(i,j) = sym(['lnp' num2str(i) num2str(j)],'real');
    end
end

for i = 1:nsample
    for j = 1:nlabel
        p(i,j) = sym(['p' num2str(i) num2str(j)],'real');
    end
end

%  d .* lnd: nsample * nlabel
%  (d.*lnd) *lambda : [nsample * nlabel] [nlabel * nlabel] ==> nample *
%  nlabel

%%%%%%%%%%%%%%%%%%%%%%%%%  第一项 %%%%%%%%%%%%%%%%%%%%%%%%%%
standard_result = 0;
for i = 1:nsample
    for j = 1:nlabel
        for m = 1:nlabel
            standard_result = standard_result + lambda(j,m)* d(i,j)* lnd(i,j);
        end
    end
end

% 版本1
sum_lambda = sum(lambda,2);
result1 = sum(sum_lambda'*(d.*lnd)');
disp(simplify(result1 - standard_result));

% 版本2,版本1和版本2得到同样的结果
result1 = sum(sum(lambda'*(lnd' .* d')));
disp(simplify(result1 - standard_result));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 第二项 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
standard_result = 0;
for i = 1:nsample
    for j = 1:nlabel
        for m = 1:nlabel
            standard_result = standard_result + lambda(j,m)*d(i,j)* lnp(i,m);
        end
    end
end
result2 = lambda*lnp'.*d';
result2 = sum(sum(result2));
disp(simplify(result2 - standard_result));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 第三项 %%%%%%%%%%%%%%%%%%%%%%%%
result3 = lambda*p'.*lnd';
result3 = sum(sum(result3));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 第四项 %%%%%%%%%%%%%%%%%%%%%%%%
standard_result = 0;
for i =1:nsample
    for j = 1:nlabel
        for m = 1:nlabel
            standard_result = standard_result + lambda(j,m)*p(i,m)*lnp(i,m);
        end
    end
end

result4 = sum(sum(lambda*(p.*lnp)'));
disp(simplify(result4 - standard_result));

% 四个表达式为:
result1 = sum(sum(lambda'*(lnd' .* d')));  % sum_lambda = sum(lambda,2);
result2 = -sum(sum(lambda*lnp'.*d'));   % nlabel * nsample
result3 = -sum(sum(lambda*p'.*lnd'));   % nlabel * nsample ,注意!点乘和叉乘不能交换位置。
result4 = sum(sum(lambda*(p.*lnp)'));  % nlabel * nsample

result_all1 = sum(sum(lambda'*(lnd' .* d') -lambda*lnp'.*d' - lambda*p'.*lnd' + lambda*(p.*lnp)' ));
tresult = result1 + result2 + result3 + result4;
% 总的表达式
standard_result = 0;
for i = 1:nsample
    for j = 1:nlabel
        for m = 1:nlabel
            standard_result = standard_result + lambda(j,m) * ( (d(i,j) - p(i,m))   * (lnd(i,j) - lnp(i,m)) );
        end
    end
end

disp('-----------------------');
disp(simplify(tresult - standard_result));
disp(simplify(result_all1 - standard_result));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值