This can be interpreted as summing over an index of a tensor when the vector $x$ is reshaped into a box of numbers instead of a list. In particular, if $X$ is the $d\text{-by-}d$ folded version of $x$, then the operation you are doing is,
\begin{align}
Dx &= \mathrm{vec}\left((I \otimes \mathbf{1})\mathrm{vec}(X)\right) \\
&= \mathrm{vec}(\mathbf{1}^T X I) \\
&= \mathrm{vec}(\mathbf{1}^T X).
\end{align}
The matlab code to do this is surprisingly simple. It is,
sum(reshape(x,d,d))'
Here's an example of it in action - you can see that it far outperforms the standard dense multiply, sparse matrix multiply, and for loop versions:
>> onesmatrixquestion
dense matrix multiply
Elapsed time is 0.000873 seconds.
sparse matrix multiply
Elapsed time is 0.000115 seconds.
for loop version
Elapsed time is 0.000154 seconds.
tensorized version
Elapsed time is 0.000018 seconds.
Here's the code that generated those timing results:
%onesmatrixquestion.m
d = 100;
onevec = ones(1,d);
D = kron(eye(d,d),onevec);
Dsparse = sparse(D);
x = randn(d^2,1);
disp('dense matrix multiply')
tic
aa = D*x;
toc
disp('sparse matrix multiply')
tic
bb = Dsparse*x;
toc
disp('for loop version')
tic
cc = zeros(d,1);
ind = 1;
for kk=1:d
for jj=1:d
cc(kk) = cc(kk) + x(ind);
ind = ind + 1;
end
end
toc
disp('tensorized version')
tic
dd = sum(reshape(x,d,d))';
toc
if (norm(aa - bb) > 1e-9 || norm(bb - cc) > 1e-9 ...
|| norm(cc - dd) > 1e-9)
disp('error: different methods give different results')
end