在matlab中删除具有相同第一列值的行

发布于 2024-11-09 08:55:55 字数 181 浏览 0 评论 0原文

我在 Matlab 中有一个大小为 10000 X 3 的单元矩阵,我想删除第一列中具有相同值的行。

也就是说,如果第 i 行和第 j 行在第一列中具有相同的值,我想删除这两行。

我还应该说,第一列中可能有两行以上具有相同的值,在这种情况下,我想删除所有这些行。

我该怎么做?

谢谢!

I have a cell matrix of size 10000 X 3 in Matlab and I would like to remove rows with the same value in the first column.

That is, if row i and row j have the same value in the first column, I'd like to delete both rows.

I should also say that there can be more than two rows with the same value in the first column and in that case, I'd like to delete all these rows.

How do I do it?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪菊怪哟 2024-11-16 08:55:55

您可以使用函数 histc, < a href="http://www.mathworks.com/help/techdoc/ref/unique.html" rel="nofollow">唯一和逻辑索引来实现你想要的。这是一个小例子。

a=randi(10,5,3)         %#generate a sample random matrix

a =

     5     3     5
     5     7    10
     7     7     4
     8     2     6
     8     2     3

[uniqVals,uniqIndx]=unique(a(:,1));  %# get unique values and corresponding indices of the first column of a
count=histc(a(:,1),uniqVals); %# get the bin counts of the elements (i.e., find which are repeated)

b=a(uniqIndx(count==1),:)

b =

     7     7     4

仅选择具有非重复元素的行。既然您说您有一个单元矩阵,只需使用 cell2mat 在执行此操作之前。

You can use the functions histc, unique and logical indexing to achieve what you want. Here's a small example.

a=randi(10,5,3)         %#generate a sample random matrix

a =

     5     3     5
     5     7    10
     7     7     4
     8     2     6
     8     2     3

[uniqVals,uniqIndx]=unique(a(:,1));  %# get unique values and corresponding indices of the first column of a
count=histc(a(:,1),uniqVals); %# get the bin counts of the elements (i.e., find which are repeated)

b=a(uniqIndx(count==1),:)

b =

     7     7     4

Only the row with the non-repeated element is selected. Since you said that you have a cell matrix, simply covert it to a matrix using cell2mat before doing this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文