Create a Matrix - Replace elements of a Matrix

조회 수: 3 (최근 30일)
Konstantinos
Konstantinos 2014년 12월 13일
댓글: Konstantinos 2014년 12월 13일
I want to create a row of a matrix which consist of 16 zeros and random replace 4 of the first 8 elements with ones and 2 of the last 8 elements with ones.
i.e A = [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;]
Then B= [ 1 0 0 1 1 0 0 1 0 0 1 0 0 1 0 0 ; ]
The ones should be replaced randomly in their positions as explained before.
Any help could be useful.
Thanks in advance !

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 12월 13일
A = [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
B=A
B([randperm(8,4) randperm(8,2)+8])=1
  댓글 수: 3
Azzi Abdelmalek
Azzi Abdelmalek 2014년 12월 13일
You can write
A=zeros(1,16)
instead of
A = [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
Konstantinos
Konstantinos 2014년 12월 13일
Indeed...my problem was that I didn't know the existance of "randperm" command.

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

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by