필터 지우기
필터 지우기

Check and replace elements in a matrix

조회 수: 2 (최근 30일)
Konstantinos
Konstantinos 2015년 1월 9일
댓글: Konstantinos 2015년 1월 9일
I have a random generated matrix (2x8) which consist of zeros and ones. How can I check, in each row, if there are 2 ones in the first 4 elements and 1 one in the last 4 elements ? Moreover, If there are more ones than the prescribed, I would like to exchange the redundants ones with zeros.
i.e rand = ( 1 1 1 0 0 1 1 0; 0 1 1 1 0 1 0 0 )
modified_rand = ( 1 1 0 0 0 1 0 0; 0 1 1 0 0 1 0 0 )
Any help could be useful. Thanks in advance !

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2015년 1월 9일
a=[ 1 1 1 0 0 1 1 0; 0 1 1 1 0 1 0 0 ]
a1=a(:,1:4);
a2=a(:,5:end)
a1(cumsum(a1,2)>2)=0
a2(cumsum(a2,2)>1)=0
b=[a1 a2]
  댓글 수: 2
Konstantinos
Konstantinos 2015년 1월 9일
Thanks a lot!
Konstantinos
Konstantinos 2015년 1월 9일
If there are more ones than the prescribed number, is it possible to find random which ones to keep and which ones to replace with zeros ?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by