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
enda = 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