Re: [julia-dev] Scalars and 1x1 arrays

131 views
Skip to first unread message

Stefan Karpinski

unread,
Aug 16, 2012, 10:13:47 AM8/16/12
to juli...@googlegroups.com
That's correct, 1x1 matrices and scalars are different. Multiplying a matrix by a scalar is fine — it scales the matrix. Multiplying a matrix by a 1x1 matrix is an error unless the matrix has a corresponding dimension of size 1.

On Aug 16, 2012, at 12:03 AM, Tirthankar Chakravarty <tirthank...@gmail.com> wrote:


I am trying to code up some examples in Julia and have some questions regarding arrays and scalars. Here is Matlab code that works:
%==============================
clc; clear;
vY = randn(100, 1);  % outcome variable
mX = randn(100, 4);  % design matrix
iN = size(vY, 1); % sample size
vBeta = (vY\mX)';  % estimated coefficients
vE = vY - mX*vBeta;  % residuals
dSigmaSq = vE'*vE/iN;  % residual variance
mV = dSigmaSq.*(inv(mX'*mX)); % covariance matrix 
vStdErr = diag(mV);  % std. err. 
vT = (sqrt(iN)*vBeta)./vStdErr;  % t-statistics
[vBeta, vStdErr, vT]
%==============================
On line 7, Matlab understands that the object "dSigmaSq" is a real scalar.

However, Julia needs this value to be explicitly dealt out of the 1x1 array,
#===============================
vY = randn(100, 1);  # outcome variable
mX = randn(100, 4);  # design matrix
iN = size(vY, 1); # sample size
vBeta = (vY\mX)';  # estimated coefficients
vE = vY - mX*vBeta;  # residuals
dSigmaSq = vE'*vE/iN;  # residual variance
mV = dSigmaSq[1,1].*(inv(mX'*mX));  # covariance matrix; dSigmaSq.* 
vStdErr = diag(mV)  # std. err.
vT = (sqrt(iN).*vBeta)./vStdErr  # t-statistics
println([vBeta'; vStdErr'; vT']')
#===============================

Is this an intended design feature? I am wondering if my expectations are skewed by Matlab's behaviour.

Thanks.

--
 
 
 
Reply all
Reply to author
Forward
0 new messages