필터 지우기
필터 지우기

Grouping matched data by row and column

조회 수: 1 (최근 30일)
Matlab User
Matlab User 2016년 3월 23일
댓글: Matlab User 2016년 3월 24일
I have a matrix attached, Rows_col.mat. This gives the Row and column of some other matrix that has matched a condition I have imposed. So for example, 6 has matched with 1, 19 has matched with 7. However there are occasions where there are more matches, for example 9 has matched with 3, but 17 has also matched with 3. In this instance I would like the output to be [9, 17, 3] (the order is not important). I think the output in a cell array would be the most suitable since there is a varying matrix column size for each "match". I hope this is clear, This for example would be the desired output:
[6,1];[9,3,17 (any order would be fine)];[19,7];[15,10,23];[12,11,18];[21,13];[20,14], this would be a 7x1 cell.
Thankyou.
  댓글 수: 3
Guillaume
Guillaume 2016년 3월 23일
In your example you have several rows matching the same column. Could you also have several columns matching the same row, i.e:
6 1
6 2
Or even a mixture of two:
6 1
6 2
5 2
which would give a permutation of [1 2 5 6]?
In your output example of [9 3 17] are you sure you don't care anymore if a number comes from a row or column?
Matlab User
Matlab User 2016년 3월 23일
I'm not sure that my explanation was good enough. I would like, for example you see in my matrix that there is a [9, 3] pair and also a [17, 3]. They both share a common value, 3, so I would like to concatenate the elements, so now [9,3,17] are all in one. When it comes to the order, I meant that it would be fine to have [3 9 17],[9 ,3,17 ],[17,3,9]...ETC, so long as all three (in this case) elements are there.

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2016년 3월 23일
편집: Guillaume 2016년 3월 23일
I don't think that there is any other way than using loops to solve this. This works:
Rows_col_ = [6 1; 9 3; 17 3; 19 7; 17 9; 15 10; 23 10; 12 11; 18 11; 18 12; 21 13; 20 14; 23 15];
sequences = {};
for row = Rows_col_' %iterate over the rows of Rows_Cols
hascommonvalue = cellfun(@(s) any(ismember(row, s)), sequences); %check if current row has any value common to a stored sequence
if any(hascommonvalue)
%current row has a common value with one or more sequence already stored, group all together
newmatch = unique([sequences{hascommonvalue}, row']); %concatenate all matching sequences and current row
sequences(hascommonvalue) = []; %remove matching sequences since they've been concatenated as a new sequence
sequences{end+1} = newmatch; %and add new sequence
else
%current did not match any previous sequence, add as new
sequences{end+1} = row';
end
end
  댓글 수: 1
Matlab User
Matlab User 2016년 3월 24일
Thankyou, I was trying to avoid loops but this works very well so thanks again.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by