how i can delete useless data from matrix

2 次查看(过去 30 天)
hi guys,
i have a matrin m lines 11 columns.
i wish a new matrix where are deleted the lines when:
there are three lines row the same of the 8th columns and how the second condition the same but with 6 column. Like the the example in the insert.
from the 715 lines there is the problem that i would deley in the new matrix.
thanks
  8 个评论
Giuseppe Antonacci
sorry but the matrix is too big. i would remove the duplicates with the idea. when i have the same number for three lines. i think that i use a cycle for/if.
Guillaume
Guillaume 2019-2-5
It's very unlikely that you need loops and if statement. If the matrix is too big, attached a smaller one which still has enough data to show what you want.
I'm with Bob, I'm really unclear on what you want. So attach a simple example of input and the corresponding desired output.

请先登录,再进行评论。

回答(2 个)

Bob Thompson
Bob Thompson 2019-2-5
编辑:Bob Thompson 2019-2-5
Here is something to try. Probably not the most efficient, but it should do what you're asking.
vals = unique(data(:,8));
data2 = [];
for i = 1:length(vals);
if size(data(data(:,8)==vals(i),:),1)<=3
data2 = [data2; data(data(:,8)==vals(i),:)];
else
tmp = data(data(:,8)==vals(i),:);
data2 = [data2; tmp(1:3,:)];
end
end
I would agree with Guillaume that extra explanation would be helpful. If this loop combo does what you're looking for we can probably come up with some way of doing this without the loops, just let us know.

Stephen23
Stephen23 2019-2-6
编辑:Stephen23 2019-2-6
Do NOT use a loop for this.
MATLAB code should be neat and simple:
[~,idx] = unique(data(:,[6,8]),'stable','rows');
out = data(idx,:)
Because you have floating point numbers, I recommend either using uniquetol, or scale the values and round them before unique.
  3 个评论
Stephen23
Stephen23 2019-2-7
编辑:Stephen23 2019-2-7
Download Jan Simon's excellent RunLength here:
and use it like this:
>> M = [21,42,38,29;29,39,41,29;23,3,2,18;15,3,2,31;24,3,2,48;51,35,24,22;21,42,38,29;29,39,41,29;23,3,2,18;15,3,2,31;51,35,24,22]
M =
21 42 38 29
29 39 41 29
23 3 2 18
15 3 2 31
24 3 2 48
51 35 24 22
21 42 38 29
29 39 41 29
23 3 2 18
15 3 2 31
51 35 24 22
>> R = 3; % >=R identical rows will be deleted.
>> X = all(diff(M(:,[2,3]),1,1)==0,2); % check columns 2 & 3.
>> [B,N] = RunLength([false;X]|[X;false]);
>> B(N(:)<R & B) = false;
>> Y = repelem(B,N)
>> M(Y,:) = [] % remove rows
M =
21 42 38 29
29 39 41 29
51 35 24 22
21 42 38 29
29 39 41 29
23 3 2 18
15 3 2 31
51 35 24 22
Giuseppe Antonacci
(undefined function or variable "RunLength"). why?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by