将Matlab转换为R给出非一致性阵列
我需要将一个Matlab脚本移植到R 下面是一个片段:将Matlab转换为R给出非一致性阵列,r,matlab,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);
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
我弄错了哪些部分
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] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
其余的呢
1 %*% matrix(helperTerm2,1,length(helperTerm2))
给出一个1x5矩阵:
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 1 1 1 1 1
[3,] 2 2 2 2 2
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
您应该添加具有相同维度的矩阵