无需 IML (SAS) 即可计算矩阵乘法
我从“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论