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일

0 개 추천

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일

2 개 추천

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

댓글 수: 3

Stephen23
Stephen23 2015년 5월 5일
Very nice!
Offroad Jeep
Offroad Jeep 2015년 5월 5일
Thanks.........
Andrei Bobrov
Andrei Bobrov 2015년 5월 5일
please accept my answer too! :)

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2015년 5월 5일

댓글:

2015년 5월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by