How can I compare a set of variables?

7 ビュー (過去 30 日間)
Seba.V
Seba.V 2019 年 8 月 22 日
回答済み: CC LAI 2020 年 3 月 31 日
Given a set of variables:
x1= 1;
x2= 3;
x3= 4;
x4= 3;
x5= 3;
x6= 1;
x7=2;
Is there a way in MATLAB to scan through the variables and identify which one are equal and output something like this?
x1=[1 1];
x2=[3 3 3];
x3=[4];
x7=[2];

回答 (3 件)

Andrei Bobrov
Andrei Bobrov 2019 年 8 月 22 日
x= [1;
3;
4;
3;
3;
1;
2]
[~,~,c] = unique(x,'stable');
x_out = accumarray(c,x,[],@(x){x});

KSSV
KSSV 2019 年 8 月 22 日
x1= 1;
x2= 3;
x3= 4;
x4= 3;
x5= 3;
x6= 1;
x7=2;
x = [x1 x2 x3 x4 x5 x6 x7] ;
[c,ia,ib] = unique(x) ;
iwant = cell(length(c),1) ;
for i = 1:length(c)
iwant{i} = x(ib==i) ;
end
celldisp(iwant)

CC LAI
CC LAI 2020 年 3 月 31 日
x1= 1;
x2= 3;
x3= 4;
x4= 3;
x5= 3;
x6= 1;
x7=2;
x = [x1 x2 x3 x4 x5 x6 x7] ;
[c,ia,ib] = unique(x) ;
iwant = cell(length(c),1) ;
for i = 1:length(c)
iwant{i} = x(ib==i) ;
end
celldisp(iwant)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by