MATLAB: Matrix with alternating signs in each row vector

matrix

Hi Guys,
Is there a way to improve on this code that I wrote to optimize it?
M = zeros(M,N); % create an MxN matrix
M(1,:) = 1; % Set first row to 1
for r = 2:I
M(r,:) = -M(r-1,:); %sets alternate rows to -1 and +1
end
a = M * diag(1 2 3 4 5);
so M creates:
M =
1 1 1 1 1
-1 -1 -1 -1 -1
1 1 1 1 1
-1 -1 -1 -1 -1
1 1 1 1 1
-1 -1 -1 -1 -1
1 1 1 1 1
-1 -1 -1 -1 -1
and a
a =
1 2 3 4 5
-1 -2 -3 -4 -5
1 2 3 4 5
-1 -2 -3 -4 -5
1 2 3 4 5
-1 -2 -3 -4 -5
1 2 3 4 5
-1 -2 -3 -4 -5
Is this the fastest and most efficient implementation to get the above? Thanks!

Best Answer

Some improvement.
m=5;n=4;
M=ones(m,n);
M(2:2:end,:)=-1
Or alternative:
m=9;n=8;
a=(2*mod((1:m)',2)-1)*(1:n)