无需 IML (SAS) 即可计算矩阵乘法

发布于 2025-01-11 06:16:30 字数 1535 浏览 0 评论 0原文

我从“Dmitry Shopin”的答案中找到了无需 IML 即可计算矩阵乘法的方法。 https://stackoverflow.com/a/22007409/17754000

%macro prod_mat_merge(in_A =,in_B=,ou_AB=);
/*determine number of rows and columns in the 2nd matrix*/
%let B_id=%sysfunc(open(&in_B));
%let B_rows=%sysfunc(attrn(&B_id,nobs));
%let B_cols=%sysfunc(attrn(&B_id,nvars));
%let rc=%sysfunc(close(&B_id));

/*transpose the 2nd matrix*/
proc transpose data=&in_B out=t&in_B(drop=_:);run;

/*making Cartesian product of the 1st and transposed 2nd matrices*/
data &ou_AB;
    do until(eofA);
        set &in_A end=eofA;
        do i=1 to n;
            set t&in_B nobs=n point=i;
            output;
        end;
    end;
run;

/*multiplication*/
data &ou_AB;
    /*new columns for products, equal to number of columns in the 2nd matrix*/
    array p[&B_cols];
    do j=1 to &B_cols;
        p[j]=0;
        set &ou_AB;
        array col _ALL_;
        /*multiply corresponding pairs of columns*/
        do i=&B_cols+2 to &B_cols+1+&B_rows;
            p[j]+col[i]*col[i+&B_rows];
        end;
    end;
    output;
    keep p:;
run;
%mend prod_mat_merge;

但是,我希望 P[j] 都是相同,因为 i 的循环独立于 j。

    do i=&B_cols+2 to &B_cols+1+&B_rows;
        p[j]+col[i]*col[i+&B_rows];
    end;

我还认为数组 P 应该具有 (A_rows*&B_cols) 行和 (&B_cols) 列。

但结果 P 是一个 (A_rows × &B_cols) 矩阵。

这是怎么发生的?

I found the way to compute matrix multiplication without IML from the answer of 'Dmitry Shopin'.
https://stackoverflow.com/a/22007409/17754000

%macro prod_mat_merge(in_A =,in_B=,ou_AB=);
/*determine number of rows and columns in the 2nd matrix*/
%let B_id=%sysfunc(open(&in_B));
%let B_rows=%sysfunc(attrn(&B_id,nobs));
%let B_cols=%sysfunc(attrn(&B_id,nvars));
%let rc=%sysfunc(close(&B_id));

/*transpose the 2nd matrix*/
proc transpose data=&in_B out=t&in_B(drop=_:);run;

/*making Cartesian product of the 1st and transposed 2nd matrices*/
data &ou_AB;
    do until(eofA);
        set &in_A end=eofA;
        do i=1 to n;
            set t&in_B nobs=n point=i;
            output;
        end;
    end;
run;

/*multiplication*/
data &ou_AB;
    /*new columns for products, equal to number of columns in the 2nd matrix*/
    array p[&B_cols];
    do j=1 to &B_cols;
        p[j]=0;
        set &ou_AB;
        array col _ALL_;
        /*multiply corresponding pairs of columns*/
        do i=&B_cols+2 to &B_cols+1+&B_rows;
            p[j]+col[i]*col[i+&B_rows];
        end;
    end;
    output;
    keep p:;
run;
%mend prod_mat_merge;

However, I expect that P[j] will all be the same because the loop for i is independent of j.

    do i=&B_cols+2 to &B_cols+1+&B_rows;
        p[j]+col[i]*col[i+&B_rows];
    end;

Also I think that the array P should have (A_rows*&B_cols) rows and (&B_cols) columns.

But the result P is a (A_rows × &B_cols) matrix.

How does it happen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文