가중치가 두 번 적용돼요. 이 줄을 보세요:
res = (obs - pred) .* w;
그다음 res를 제곱하니까 분자에는 w^2이 들어가는데, 분모는 sum(w)뿐이에요. 전부 1이면 차이가 없어서 괜찮아 보이는 거예요. Fortran 루프는 분명 w(i)*r(i)**2일 거예요.
해결: 먼저 제곱하고 나서 가중치를 곱하세요.
res = obs - pred;
s = sqrt(sum(w .* res.^2) / sum(w));
shape도 확인하세요. obs가 열 벡터이고 w가 행 벡터면 .*와 -가 행렬로 브로드캐스트되는데, 에러는 안 나와요. 세 개 모두 같은 방향으로 맞추세요 (w(:), obs(:), pred(:)).
스칼라에 mean(wss)를 해도 아무 일도 안 일어나요. 채널이 여러 개면 먼저 열마다 sum(..., 1)을 한 다음에 어떻게 평균낼지 정하세요.
차원 인자를 쓰는 sum 문서: https://www.mathworks.com/help/matlab/ref/sum.html
영어에서 번역·원문 보기Weight is applied twice. Look at this line:
res = (obs - pred) .* w;
Then you square res, so numerator gets w^2, but denominator is sum(w) only. With all ones no difference, so it looks fine. Your Fortran loop surely does w(i)*r(i)**2.
Fix: square first, then weight.
res = obs - pred;
s = sqrt(sum(w .* res.^2) / sum(w));
Check shapes too. If obs is a column and w is a row, .* and - broadcast to a matrix, and no error is shown. Make all three the same orientation (w(:), obs(:), pred(:)).
mean(wss) of a scalar does nothing. For several channels, do sum(..., 1) per column first, then decide how to average.
Docs for sum with dimension argument: https://www.mathworks.com/help/matlab/ref/sum.html