-1 and 1 matrix

조회 수: 2 (최근 30일)
Offroad Jeep
Offroad Jeep 2015년 5월 5일
댓글: Andrei Bobrov 2015년 5월 5일
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.........

채택된 답변

Stephen23
Stephen23 2015년 5월 5일
편집: Stephen23 2015년 5월 5일
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

추가 답변 (2개)

Andrei Bobrov
Andrei Bobrov 2015년 5월 5일
-(-1).^(1:5)'*ones(1,5);
  댓글 수: 3
Offroad Jeep
Offroad Jeep 2015년 5월 5일
Thanks.........
Andrei Bobrov
Andrei Bobrov 2015년 5월 5일
please accept my answer too! :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by