Collect double values of a matrix

조회 수: 12 (최근 30일)
Tobias Riewendt
Tobias Riewendt 2019년 10월 7일
댓글: Tobias Riewendt 2019년 10월 9일
Hi,
I have got a question. I have a matrix with three columns (X,Y,Z coordinates) and nearly 60000 rows. Now, I would like to compare the X and Z values of all 60000 rows. All rows whose X and Z values only occur once should be deleted, only the rows whose X and Z value occur twice or more should be regarded. Next, the rows with the equal X and Z value should be compared and the row with the higher Y value should be written into a new matrix.
I thought about using a loop for the search of the rows with equal X and Z values but the computing time is quite high, is there a better way? And how do I define the "do"?
for k = 1:end
for m = k+1:end
if A(k,1) == A(m,1) && A(k,3) ==A(m,3)
do something
end
end
end
I haven't thought about the loop for the search of the higher Y value, yet.
Yours,
Tobias

채택된 답변

Andrei Bobrov
Andrei Bobrov 2019년 10월 9일
편집: Andrei Bobrov 2019년 10월 9일
Let xyz - your array (n x 3);
[~,~,c] = unique(xyz(:,[1,3]),'rows','stable'); % c - serial numbers of rows with same values in first and last columns.
d = accumarray(c,1); % d - the number of specific rows with the same first and last columns. Here first row of 'd' c = 1, nth row - c = n.
[lo,i] = ismember(c,find(d > 1)); % duplicate rows in xyz.
j = accumarray(i(lo),find(lo),[],@(x)x(max(xyz(x,2)) == xyz(x,2))); % index of the row with the maximum value in the second column.
out = xyz(j,:);
  댓글 수: 3
Andrei Bobrov
Andrei Bobrov 2019년 10월 9일
Please read about unique, accumarray, ismember.
Tobias Riewendt
Tobias Riewendt 2019년 10월 9일
Ok, I will do it. Thanks for your answer.

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

추가 답변 (2개)

Martin C.
Martin C. 2019년 10월 8일
hmm well to count occurences you could somehow use a hashmap
containers.Map in matlab. that would be efficient.

Turlough Hughes
Turlough Hughes 2019년 10월 8일
편집: Turlough Hughes 2019년 10월 9일
Ok Following your comment I've edited my answer:
I would first find rows which have equal X and Z values.
c=1;
for ii=1:size(A,1)
if A(ii,1)==A(ii,3)
id_s(c)=ii;
c=c+1;
end
end
B=A(id_s,:);
Rows in A with equal X and Z value and are assigned to B.
To ensure a given X (and hence a given Z) of a row is repeated in another row I find the rows where this is not the case and delete them.
[~,ix] = unique(B(:,1)); % so B(ix,1) includes all values in B(:,1) without repeating any.
Xtw=B(:,1);
Xtw(ix)=[]; % Xtw - includes all values in X that occurred two or more times.
[~,i1x]=setdiff(B(:,1),Xtw); % Gets index for rows in B where the x value is not repeated elsewhere.
B(i1x,:)=[];
  댓글 수: 1
Tobias Riewendt
Tobias Riewendt 2019년 10월 9일
That is not exactly what I want. I try to give an example:
[X , Y , Z]:
[1 2 3
2 5 3
7 7 7
7 7 9
1 5 3]
The result should be a matrix of the rows with an equal X AND Z value. All other rows should be deleted. This means:
[1 2 3
1 5 3]

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by