How to find repeated values in a matrix?

조회 수: 2 (최근 30일)
Atiqah Zakirah
Atiqah Zakirah 2017년 5월 19일
댓글: Stephen23 2017년 5월 19일
I am trying to obtain the repeated values in each row from a matrix and then store it in a separate matrix. I'm thinking of using unique and histc functions to do so.
Q = [ 27.1028 32.3493 28.5714 28.5714; 17.1429 17.1429 18.4581 12.9200]
The repeated values in row 1 is 28.5712, in row 2 it is 17.1429. I want to obtain these values so P = [28.5712; 17.1429]. One problem I'm facing is using the unique function. unique(Q) gives me ALL the values. Why is it so?

채택된 답변

Rik
Rik 2017년 5월 19일
Are repeated values always next to each other? If so you can use diff(Q,1,2) to find the positions that have repeated values. (about that syntax: the 1 is the number of times diff will be run recursively, the 2 is the dimension along which diff should operate)
unique gives all values that occur in the vector/matrix, which is about the opposite of what you want. You can however use that: unique can return the indices of the values it finds. You can then find out what indices are missing to find the repeated values. This method relies on values not occuring in multiple rows and doesn't keep information about in which row the values were (except in idx)
Q = [ 27.1028 32.3493 28.5714 28.5714; 17.1429 17.1429 18.4581 12.9200];
[C,idx]=unique(Q,'stable');%don't sort
idx=setxor(idx,1:numel(Q));
repeating_values=Q(idx);
  댓글 수: 2
Atiqah Zakirah
Atiqah Zakirah 2017년 5월 19일
Thanks for the help! However, it only outputs the repeated values sometimes. Sometimes, it only outputs only one of the repeated values or none at all.
This is my full script. I got most of the code from online resources (I'm still trying to understand it). The code is meant to plot grid lines, randomly plot a circle, and then obtain the points where the circle intersects the grid lines. Getting the coordinate of intersects work fine. Values are stored in 'Q'. The problem now is just obtaining repeated values in each row of Q.
N = 8; % number of grid boxes per row and column
x = linspace(0,40,N) ;
y = linspace(0,40,N) ;
[X,Y] = meshgrid(x,y) ;
figure
hold on
plot(X,Y,'r') ;
plot(Y,X,'r')
%%draw circles
nc = 1 ; % number of circles
R = 3. ;
th = linspace(0,2*pi) ;
iwant = cell(nc,1) ;
for i = 1:nc
% get random centre of circle
cx0 = (max(x)-min(x)).*rand + min(x);
cy0 = (max(y)-min(y)).*rand + min(y);
% circle coordinates
cx = cx0+R*cos(th) ;
cy = cy0+R*sin(th) ;
plot(cx,cy,'k')
% get intersection
Q = cell(1,[]) ;
count = 0 ;
% loop along lines parallel to x-axes
for j = 1:N
P = InterX([cx ;cy],[X(j,:) ; Y(j,:)]) ;
if ~isempty(P)
count = count+1 ;
Q{count} = P ;
end
end
% loop along lines parallel to y-axes
for k = 1:N
P = InterX([cx ;cy],[X(:,k)' ; Y(:,k)']) ;
if ~isempty(P)
count = count+1 ;
Q{count} = P ;
end
end
Q = cell2mat(Q) ;
plot(Q(1,:),Q(2,:),'.b')
iwant{i} = Q ;
end
%%find coordinate of grid point
[C,idx]=unique(Q,'stable'); % don't sort
idx=setxor(idx,1:numel(Q));
grid_coordinates=Q(idx);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by