combining matrix rows with repeated elements

I have a matrix which its some rows have one same elements. I want to combine this rows and save the result in a cell array without repeating the elements. What is the easiest way to do so? For example, I have
A = [1 2;3 4;2 5;5 6;4 7;8 9;10 9]
And want to get,
C = {[1 2 5 6],[3 4 7],[8 9 10]}

5 件のコメント

Ben11
Ben11 2014 年 7 月 31 日
Can you please be more specific? If we look at A every row contains 2 different elements, so how do you determine which values to put in C?
dpb
dpb 2014 年 7 月 31 日
Can you not live with
C=unique(A(:)); % ?
If not, if I get the sequence above, start with those elements and their location by row. It may need be recursive given the continued elements in your first cell from the fourth row of A
Javad
Javad 2014 年 7 月 31 日
Some rows have repeated elements (e.g. 2, 5), and I want to join all these rows which have one same element two by two, without repeating the same elements. For example the first matrix in C is the combination of A(1,:)=(1 2), A(3,:)=(2 5) and A(4,:)=(5 6) that have 2 as the same element in two first ones and 5 in two last ones.
dpb
dpb 2014 年 7 月 31 日
Which is what I said...start w/ the unique values you have then look where they're located and build the array therefrom...
>> [u,ia,ib]=unique(A(:),'stable');
>> u'
ans =
1 3 2 5 4 8 10 6 7 9
>> ia'
ans =
1 2 3 4 5 6 7 11 12 13
>> ib'
ans =
1 2 3 4 5 6 7 3 5 4 8 9 10 10
>>
As you can see, you get the list of values and their locations which you can use to select which ones go where...I'll leave that as "exercise for the student"... :)
Javad
Javad 2014 年 8 月 8 日
Thanks for your point. I had a look at documentations which say u=A(ia) and A=u(ib), but I could not figure out how I can relate indices to separate them as I want. As a beginner, it's a little difficult for me to do this exercise!

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

回答 (1 件)

Jian Wei
Jian Wei 2014 年 7 月 31 日

0 投票

If I understand it correctly, you are trying to cluster the vertices in a graph into different groups according to whether they are connected or not. For general cases, the problem might be nontrivial. Please try the following code to see if it generates the results you need.
% adjacency matrix
M = zeros(max(max(10)));
for k = 1:size(A,1)
M(A(k,1),A(k,2)) = 1;
M(A(k,2),A(k,1)) = 1;
end
flag = zeros(length(M),1);
C = cell(0);
for k = 1:length(M)
if flag(k)==0
p1 = k;
flag(k) = 1;
p2 = p1;
for ki = 1:length(p1)
node = p1(ki);
p2 = union(p2,find(M(node,:)==1));
end
while length(p2)~=length(p1)
p1 = p2;
p2 = p1;
for ki = 1:length(p1)
node = p1(ki);
p2 = union(p2,find(M(node,:)==1));
end
end
for ki = 1:length(p2)
flag(p2(ki)) = 1;
end
C{end+1} = p2;
end
end
C is the cell which contains different groups of vertices that are connected with each other.

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2014 年 7 月 31 日

コメント済み:

2014 年 8 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by