How to separate a matrix randomly

Hello everyone, I want to ask how to separate a matrix randomly. For example, I have a matrix
A = [...
0 0 0 0
0 0 0 0
1 1 1 1
1 1 1 1]
and I need a C1 and C2 matrix:
C1 =
0 0 0 0
0 0 0 0
1 0 0 1
0 1 1 0
C2 =
0 0 0 0
0 0 0 0
0 1 1 0
1 0 0 1
Is it possible we can generate them by separating the 1 digit randomly?
Thanks before :)

 採用された回答

Stephen23
Stephen23 2014 年 12 月 1 日

1 投票

Randomly allocate all values from one matrix to two other matrices:
>> A = [0,0,0,0;0,0,0,0;1,1,1,1;1,1,1,1];
>> B = 0.5<rand(size(A));
>> C1 = zeros(size(A)); C2 = zeros(size(A));
>> C1(B) = A(B);
>> C2(~B) = A(~B);
If you only require the last two rows of values, then you can "&" the index with another logical matrix giving the restrictions that you require.

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2014 年 12 月 1 日

0 投票

C1 = randi([0 1],size(A)).*A;
C2 = A - C1;

カテゴリ

ヘルプ センター および File ExchangeImage Processing Toolbox についてさらに検索

質問済み:

2014 年 11 月 30 日

回答済み:

2014 年 12 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by