How to create logical binary matrix where two variables are from the same row ?

2 ビュー (過去 30 日間)
SaraJr
SaraJr 2016 年 8 月 5 日
コメント済み: SaraJr 2016 年 8 月 5 日
Hi .. let's say I have this table
*X* *Y*
01 a
01 b
01 c
02 b
02 g
03 a
03 g
04 a
04 b
04 c
I want to create a logical binary matrix where the rows indicate to the unique values of X and the columns indicate the unique values of Y the output would be like this
a b c g
01 1 1 1 0
02 0 1 0 1
03 1 0 0 1
04 1 1 1 0
I have a large table and I want to find groups of Xs based on Ys. According to this example, I have 3 groups (01,04) (02) (03).
any help would be appreciated thank you

採用された回答

Stephen23
Stephen23 2016 年 8 月 5 日
編集済み: Stephen23 2016 年 8 月 5 日
You could use unique and sub2ind:
V = [1,1,1,2,2,3,3,4,4,4];
C = {'a','b','c','b','g','a','g','a','b','c'};
%
[Cu,~,Cc] = unique(C);
[Vu,~,Vr] = unique(V);
out = false(numel(Vu),numel(Cu));
out(sub2ind(size(out),Vr,Cc)) = true
Creates:
>> out
out =
1 1 1 0
0 1 0 1
1 0 0 1
1 1 1 0
>> Cu % columns
Cu =
'a' 'b' 'c' 'g'
>> Vu % rows
Vu =
1 2 3 4

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by