Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从MATLAB矩阵中具体选择行_Matlab_Sorting - Fatal编程技术网

从MATLAB矩阵中具体选择行

从MATLAB矩阵中具体选择行,matlab,sorting,Matlab,Sorting,考虑以下矩阵: 1 2 1 1 1 3 1 1 2 5 2 3 2 6 2 4 2 6 2 4 2 9 0 0 3 4 5 6 3 4 1 1 3 2 0 0 3 1 1 1 . . . 我想为第1列中的每个唯一值选择第2列中具有最大值的行 例如。 答案应该是: 1 3 1 1 2 9 0 0 3 4 5 6 3 4 1 1 有什么想法吗 一些让您开始的代码: % unique values in first column col1 = unique(x(:,1)); % we

考虑以下矩阵:

1 2 1 1
1 3 1 1
2 5 2 3
2 6 2 4 
2 6 2 4 
2 9 0 0 
3 4 5 6
3 4 1 1 
3 2 0 0
3 1 1 1 
.
.
.
我想为第1列中的每个唯一值选择第2列中具有最大值的行

例如。 答案应该是:

1 3 1 1
2 9 0 0
3 4 5 6
3 4 1 1

有什么想法吗

一些让您开始的代码:

% unique values in first column
col1 = unique(x(:,1));

% we first store results in a cell array (later converted to matrix)
xx = cell(numel(col1), 1);

for i=1:numel(col1)
    % rows with the same value in column 1
    rows = x(x(:,1) == col1(i),:);

    % maximum value along column 2
    mx = max(rows(:,2));

    % store all rows with the max value (in case of ties)
    xx{i} = rows(rows(:,2)==mx,:);
end

% combine all resulting rows
xx = vertcat(xx{:});
所示矩阵的结果如下:

>> xx
xx =
     1     3     1     1
     2     9     0     0
     3     4     5     6
     3     4     1     1

一些代码可以帮助您开始:

% unique values in first column
col1 = unique(x(:,1));

% we first store results in a cell array (later converted to matrix)
xx = cell(numel(col1), 1);

for i=1:numel(col1)
    % rows with the same value in column 1
    rows = x(x(:,1) == col1(i),:);

    % maximum value along column 2
    mx = max(rows(:,2));

    % store all rows with the max value (in case of ties)
    xx{i} = rows(rows(:,2)==mx,:);
end

% combine all resulting rows
xx = vertcat(xx{:});
所示矩阵的结果如下:

>> xx
xx =
     1     3     1     1
     2     9     0     0
     3     4     5     6
     3     4     1     1
这里有一个方法:

%// Get a unique list of column 1 without changing the order in which they appear
[C1, ~, subs] = unique(M(:,1), 'stable');

%// Get the max value from column 2 corresponding to each unique value of column 1
C2 = accumarray(subs, M(:,2), [], @max);

%// Find the desired row indices
I = ismember(M(:,1:2), [C1, C2], 'rows');

%//Extract the rows
M(I, :)
这里有一个方法:

%// Get a unique list of column 1 without changing the order in which they appear
[C1, ~, subs] = unique(M(:,1), 'stable');

%// Get the max value from column 2 corresponding to each unique value of column 1
C2 = accumarray(subs, M(:,2), [], @max);

%// Find the desired row indices
I = ismember(M(:,1:2), [C1, C2], 'rows');

%//Extract the rows
M(I, :)

发生在
3 4 5 6
上的事情不是比
3 2 1 1
大吗?如果打成平手会发生什么?塞吉奥对此表示抱歉。编辑!@Dan print如果这是一个平局,老实说,我本以为这个问题会说明你在努力练习的哪一部分。我的意思是,你的问题很容易转化为元代码:
1)按第一列分成组,2)根据第二列选择内部组,3)合并
@bdecaf:对,这就是为什么我展示了明显的基于循环的解决方案。通常,每当我执行这种“数据聚合”时,就会想到丹的解决方案中所示的
accumarray
(可读性稍差,但速度肯定更快)发生在
3 4 5 6
上的事情不是比
3 2 1 1
大吗?如果出现平局会发生什么事?@Sergio对此表示抱歉。编辑!@Dan print如果这是一个平局,老实说,我本以为这个问题会说明你在努力练习的哪一部分。我的意思是,你的问题很容易转化为元代码:
1)按第一列分成组,2)根据第二列选择内部组,3)合并
@bdecaf:对,这就是为什么我展示了明显的基于循环的解决方案。通常,每当我执行这种“数据聚合”时,就会想到丹的解决方案中所示的
accumarray
(可读性稍差,但肯定更快),如果
m(:,1)
无效,您可能会遇到麻烦。。。不过修复很简单:
[C1,~,M1]=unique(M(:,1),'stable')
C2=accumarray(M1,M(:,2),[],@max)M(:,1)
不是有效的索引,您可能会遇到问题。。。不过修复很简单:
[C1,~,M1]=unique(M(:,1),'stable')
C2=accumarray(M1,M(:,2),[],@max)