Randomly deleting a 'one' in a column of a binary matrix

1 回表示 (過去 30 日間)
Tim
Tim 2012 年 10 月 11 日
Hi,
I have a matrix that looks like this:
1 1 1 1 1 1
0 1 1 1 1 1
0 0 1 1 1 1
0 0 1 1 1 1
0 0 0 1 1 1
0 0 0 1 1 1
0 0 0 0 1 1
0 0 0 0 1 1
0 0 0 0 0 1
0 0 0 0 0 1
What should I do when I randomly want to delete a 'one' in (let's say) column 4? Thanks in advance,
Tim

採用された回答

Wayne King
Wayne King 2012 年 10 月 11 日
編集済み: Wayne King 2012 年 10 月 11 日
If you want to just choose one of the 1's from the matrix to set equal to 0, then
indices = find(A>0);
chooseset = randperm(length(indices));
index = chooseset(1);
A(indices(index)) = 0;
If you really want to restrict it to just column 4
indices = find(A(:,4)>0);
chooseset = randperm(length(indices));
index = indices(chooseset(1));
A(index,4) = 0;
Recently randperm() has been updated so you can just get 1 value back.
indices = find(A(:,4)>0);
choice = randperm(length(indices),1);
index = indices(choice);
A(index,4) = 0;

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 11 日
編集済み: Azzi Abdelmalek 2012 年 10 月 11 日
A=[1 1 1 1 1 1
0 1 1 1 1 1
0 0 1 1 1 1
0 0 1 1 1 1
0 0 0 1 1 1
0 0 0 1 1 1
0 0 0 0 1 1
0 0 0 0 1 1
0 0 0 0 0 1
0 0 0 0 0 1]
m=size(A,2)
idx=randi(m,1)
A(:,idx)=[] % this will remove column number idx
%I 'm not sur what you mean by delete a one, if you want raplace them by 0
A(:,idx)=0
  4 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 11 日
編集済み: Azzi Abdelmalek 2012 年 10 月 11 日
Tim you did'nt read all the answer, at the end I said use
A(:,idx)=0
istead of
A(:,idx)=[]
which means use the below code
m=size(A,2)
idx=randi(m,1)
A(:,idx)=0
Tim
Tim 2012 年 10 月 11 日
Hi Azzi,
You're right, I did not read that part of the answer. This will however replace one of the columns with only zeros instead of take one of the '1's from a column. Wayne already helped me with that, but thanks again for your help!
Cheers, T.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by