Hi,
I actually struggle on a fast and good solution on finding columns in an 2D matrix containing same values and returning the row index of those columns.
% Example:
A = [2,2,7,3; ...
8,7,8,10; ...
4,3,5,2; ...
6,7,1,9];
Result should be 2 and 4 because in column 2 the number 7 occurs two times. Is there anything similar to unique or anything else? I try to avoid looping every column with unique since there will be a lot of matrices and bigger ones.
Thanks a lot!

댓글 수: 2

madhan ravi
madhan ravi 2019년 4월 26일
4 ???
Yea because A(2,2) and A(4,2) are the same values in column2 and I need to know the rows where the same values occur.
At the end I got a lot of matrices with thunderstorm tracks which undergo merging and splittings and I need to find the mergings. Each track is stored in a row. Merged and splitted tracks have at some timesteps (= columns) the exact same coordinates. And the first timestep can be assumed to be the merging time.

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

 채택된 답변

Adam Danz
Adam Danz 2019년 4월 26일
편집: Adam Danz 2019년 5월 2일

0 개 추천

I changed your example matrix to include a variety of duplicates.
% Example:
A = [2,2,7,3,5,0;8,7,8,10,5,0;4,3,5,3,9,0;6,7,1,9,9,1];
A =
2 2 7 3 5 0
8 7 8 10 5 0
4 3 5 3 9 0
6 7 1 9 9 1
As you can see, columns 2, 4, 5 & 6 have duplicates and column 5 has 2 sets while column 6 has three of the same value.
% identify the duplicate values
dupIdx = splitapply(@(x){histc(x,unique(x))>1}, A, 1:size(A,2));
unqVals = splitapply(@(x){unique(x)}, A, 1:size(A,2));
dupVals = cellfun(@(x,y)x(y), unqVals, dupIdx, 'UniformOutput', false);
% List the rows that contain duplicate values for each column
dupRows = splitapply(@(x,y) {find(ismember(x,[y{:}]))}, A, dupVals, 1:size(A,2));
% List the column numbers that contain duplicates
hasDup = find(~cellfun(@isempty, dupRows));
So, dupRows{2} will list the row numbers of column 2 that contain a duplicate. It's empty if col 2 has no duplicates.
hasDup is a vector of column numbers that contain a duplicate. It's empty if there are no duplicates in any column.
[UPDATE] For matlab releases before 2015b, here's the same method using cellfun() instead of splitapply()
% Example:
A = [2,2,7,3,5,0;8,7,8,10,5,0;4,3,5,3,9,0;6,7,1,9,9,1];
Acell = mat2cell(A,size(A,1),ones(1,size(A,2)));
dupIdx = cellfun(@(x){histc(x,unique(x))>1}, Acell);
unqVals = cellfun(@(x){unique(x)}, Acell);
dupVals = cellfun(@(x,y)x(y), unqVals, dupIdx, 'UniformOutput', false);
% List the rows that contain duplicate values for each column
dupRows = cellfun(@(x,y) find(ismember(x,y(:))), Acell, dupVals,'UniformOutput', false);
% List the column numbers that contain duplicates
hasDup = find(~cellfun(@isempty, dupRows));
% Alternative
% hasDup = any(([ones(1,size(A,2));diff(sort(A))]~=0)==0);

댓글 수: 3

Hi
sorry was out of office a few days. Unfortunatly splitapply is not included in 2015a release or is it in a certain toolbox?
Thank you!
Cheers
Adam Danz
Adam Danz 2019년 5월 2일
splitapply() came out in r2015b. I updated my solution to include a second method using cellfun() instead of splitapply(). It's the same logic; just adapted to cellfun().
Yeah thanks a lot, that works now!
Really need to update the matlab release on cluster due to some really good new functions in newer releases!
Thanks!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

제품

릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by