将Matlab转换为R可以得到不一致的数组

时间:2015-09-23 08:03:26

标签: r matlab

我需要将Matlab脚本移植到R。

这是一个片段:

n = 3;
M = 7;
tau = 1;
shift_mat_ind = reshape(0:tau:(n-1)*tau,[],1) * ones(1,M-(n-1)*tau) +...
    ones(n, 1) * reshape(1:(M-(n-1)*tau),1,[]);

我尝试将其转换为R:

n = 3;
M = 7;
tau = 1;
helperTerm1 = seq(0, (n-1)*tau, tau);
helperTerm2 = 1:(M-(n-1)*tau);
shift_mat_ind = matrix(helperTerm1,length(helperTerm1),1) %*% matrix(1,1,M-(n-1)*tau) + 1 %*% matrix(helperTerm2,1,length(helperTerm2));

我的Matlab知识非常有限,所以我不知道这里出了什么问题。

错误消息是:

Error in matrix(helperTerm1, length(helperTerm1), 1) %*% matrix(1, 1,  : 
non-conformable arrays

哪些部分我错了?

1 个答案:

答案 0 :(得分:0)

matrix(helperTerm1,length(helperTerm1),1) %*% 
matrix(1,1,M-(n-1)*tau)

给出3x5矩阵:

     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    0    0
[2,]    1    1    1    1    1
[3,]    2    2    2    2    2

和其他

1 %*% matrix(helperTerm2,1,length(helperTerm2))

给出1x5矩阵:

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5

您应该添加具有相同尺寸的矩阵。