짧은 MATLAB 스크립트를 R로 다시 짜 보는 중인데, MATLAB은 거의 써 본 적이 없어요. 대부분은 무난하게 옮겨졌는데 한 줄만 다른 숫자를 내요.
A = [2 0; 1 3];
w = [0.5; 0.25];
s = A * w;
B = A .* A;
A <- matrix(c(2, 0, 1, 3), nrow = 2, byrow = TRUE)
w <- c(0.5, 0.25)
s <- A * w
B <- A * A
s
#> [,1] [,2]
#> [1,] 1.00 0.00
#> [2,] 0.25 0.75
B는 MATLAB과 같아요. s는 MATLAB에서 1과 1.25 두 숫자인데, R에서는 2행 2열 행렬이 통째로 나오고 오류도 경고도 없어요. A가 양쪽에서 똑같이 출력되는 건 확인했으니 byrow 문제는 아니에요. R에는 .*가 없는 것 같아서 점을 전부 그냥 뺐어요. 거기서 잘못된 걸까요, 아니면 w를 만든 방식 때문일까요?
영어에서 번역·원문 보기Trying to redo a short MATLAB script in R, and I've barely touched MATLAB before. Most of it came over fine, except for a single line that gives different numbers.
A = [2 0; 1 3];
w = [0.5; 0.25];
s = A * w;
B = A .* A;
A <- matrix(c(2, 0, 1, 3), nrow = 2, byrow = TRUE)
w <- c(0.5, 0.25)
s <- A * w
B <- A * A
s
#> [,1] [,2]
#> [1,] 1.00 0.00
#> [2,] 0.25 0.75
B agrees with MATLAB. s in MATLAB is two numbers, 1 and 1.25, and in R it's a whole 2 by 2 matrix with no error and no warning. I checked that A prints the same in both, so byrow isn't the problem. R didn't seem to have .*, so I just dropped the dot everywhere. Is that where it went wrong, or is it something about how I built w?