Matrix with alternating signs in each row vector

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!

 Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 19 de Sept. de 2011
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)

5 comentarios

Leor Greenberger
Leor Greenberger el 19 de Sept. de 2011
Another question!
I want to do now (assuming an MX8 matrix) column by column addition with vector k transposed:
N=8
k = [0 5 5 10 10 15 15 20]
for r = 1:N
b = k' + a(:,r);
end
Is a for loop the best way?
Fangjun Jiang
Fangjun Jiang el 19 de Sept. de 2011
You just need this:
b=k'+sum(a,2)
Leor Greenberger
Leor Greenberger el 19 de Sept. de 2011
Oops. I made a mistake in phrasing my question
N=8
k = [0 5 5 10 10 15 15 20]
for r = 1:N
b(:,r) = k' + a(:,r);
end
I want to add each column vector in a with k' and store them in b.
In other words, add column 1 in a with k' and store in column 1 of b, add column 2 in a with k' and store it in column 2 of b, etc.
the cyclist
the cyclist el 19 de Sept. de 2011
I suggest that accept one of the answers here, assuming that it helped you. And make this comment into a separate question.
Fangjun Jiang
Fangjun Jiang el 19 de Sept. de 2011
For that, you can use repmat.
b=repmat(k',1,N)+a

Iniciar sesión para comentar.

Más respuestas (3)

the cyclist
the cyclist el 19 de Sept. de 2011
One of many ways to get your result:
M = 7;
N = 5;
V = (-1).^(0:M);
A = bsxfun(@times,1:N,V')

3 comentarios

Andrei Bobrov
Andrei Bobrov el 20 de Sept. de 2011
(-1).^(0:7)'*(1:5)
Jan
Jan el 20 de Sept. de 2011
The power operation is very expensive. Using MOD is much faster.
Andrei Bobrov
Andrei Bobrov el 20 de Sept. de 2011
Hi Jan! My "research"
>> t = zeros(100,2);
for j1 = 1:100
tic,(-1).^(0:1000)'*(1:100);t(j1,1)=toc;
tic,(2*rem((1:1000)',2)-1)*(1:100);t(j1,2)=toc;
end
>> [min(t);mean(t);median(t);max(t)]
ans =
0.0008 0.0006
0.0012 0.0012
0.0012 0.0010
0.0030 0.0259

Iniciar sesión para comentar.

Sean de Wolski
Sean de Wolski el 19 de Sept. de 2011
b = bsxfun(@plus,k',a(:,1:N))
to your comment in the Fangjun's answer.
Jonathan
Jonathan el 13 de Ag. de 2014

0 votos

is there a generic way of making an array of ones that alternate form +1 to -1?

1 comentario

the cyclist
the cyclist el 13 de Ag. de 2014
Why did you bury a brand-new question as a comment on a 3-year-old thread?

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 19 de Sept. de 2011

Comentada:

el 13 de Ag. de 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by