フィルターのクリア

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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by