-1 and 1 matrix

1 view (last 30 days)
Offroad Jeep
Offroad Jeep on 5 May 2015
Commented: Andrei Bobrov on 5 May 2015
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.........

Accepted Answer

Stephen23
Stephen23 on 5 May 2015
Edited: Stephen23 on 5 May 2015
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

More Answers (2)

Andrei Bobrov
Andrei Bobrov on 5 May 2015
-(-1).^(1:5)'*ones(1,5);
  3 Comments
Offroad Jeep
Offroad Jeep on 5 May 2015
Thanks.........
Andrei Bobrov
Andrei Bobrov on 5 May 2015
please accept my answer too! :)

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!