Hello All,
I want to create a n X n matrix with 1row 1, 2nd row -1, 3rd row 1 , 4th row -1 and so on..... can anyone please guide me .......... thanks.........

 Réponse acceptée

Stephen23
Stephen23 le 5 Mai 2015
Modifié(e) : Stephen23 le 5 Mai 2015

0 votes

Method One:
>> N = 5;
>> 2*mod(ndgrid(1:N,1:N),2)-1
ans =
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
Method Two:
>> 2*(ones(N,1)*mod(1:N,2)).'-1
ans =
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
Method Three:
>> repmat(2*mod(1:N,2).'-1,1,N)
ans =
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

Plus de réponses (2)

Andrei Bobrov
Andrei Bobrov le 5 Mai 2015

2 votes

-(-1).^(1:5)'*ones(1,5);

3 commentaires

Stephen23
Stephen23 le 5 Mai 2015
Very nice!
Offroad Jeep
Offroad Jeep le 5 Mai 2015
Thanks.........
Andrei Bobrov
Andrei Bobrov le 5 Mai 2015
please accept my answer too! :)

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by