MATLAB:从矩阵中删除最后 n 行,无需循环
我在删除矩阵末尾的多余行时遇到问题。一般来说,我需要删除特定列中包含特定元素的行,而不使用循环。这看起来很容易,但我仍然不断得到一些奇怪的结果。
举个简单的例子,我们有一个 10x10 矩阵 A:
A=[1:10; 901:910; 201:210; 301:310; 701:710; 401:410; 601:610; 501:510; 801:810; 101:110];
我想删除(为了更好的说明,只需替换为 101:110)从第四行到最后一行,其第三列包含的值高于 600。结果应该如下所示:
| 1 2 3 4 5 6 7 8 9 10|
|901 902 903 904 905 906 907 908 909 910|
|201 202 203 204 205 206 207 208 209 210|
|301 302 303 304 305 306 307 308 309 310|
| 1 1 1 1 1 1 1 1 1 1|
|401 402 403 404 405 406 407 408 409 410|
| 1 1 1 1 1 1 1 1 1 1|
|501 502 503 504 505 506 507 508 509 510|
| 1 1 1 1 1 1 1 1 1 1|
|101 102 103 104 105 106 107 108 109 110|
我的想法如下
A(A(4:end,3)>600,:)=[1];
但结果是一些无意义的矩阵。
感谢您的帮助!
I have a problem with removing surplus rows in the end of the matrix. In general, I need to remove rows that contain a specific elements in a specific column, without using a loop. It seems easy but I still keep on getting some weird outcomes.
For simple example, let's have a 10x10 matrix A:
A=[1:10; 901:910; 201:210; 301:310; 701:710; 401:410; 601:610; 501:510; 801:810; 101:110];
And I want to remove (for better illustration just replace with ones) that rows from fourth to the last, whose third columns contain value higher than 600. Result should look like that:
| 1 2 3 4 5 6 7 8 9 10|
|901 902 903 904 905 906 907 908 909 910|
|201 202 203 204 205 206 207 208 209 210|
|301 302 303 304 305 306 307 308 309 310|
| 1 1 1 1 1 1 1 1 1 1|
|401 402 403 404 405 406 407 408 409 410|
| 1 1 1 1 1 1 1 1 1 1|
|501 502 503 504 505 506 507 508 509 510|
| 1 1 1 1 1 1 1 1 1 1|
|101 102 103 104 105 106 107 108 109 110|
My idea looks like that:
A(A(4:end,3)>600,:)=[1];
But the outcome is some nonsense matrix.
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如@yoda所说,要删除行,请执行以下操作:
and as @yoda said, to remove the rows do:
首先,您的问题是在矩阵内输入矩阵意味着使用矩阵的值作为索引。因为你的矩阵只有零和一(由于“<”运算符),所以你得到了一个奇怪的结果。
要回答您的问题,您需要用较小的矩阵替换该矩阵。像这样的东西:
A = new_matrix
new_matrix 是通过仅获取所需的行从 A 生成的。
new_matrix = A(row_indices, :)
其中 row_indices 是要保留的索引向量。要构建它,您可以从零-一向量开始并应用 find (这将产生带有 1 的索引)。所以我们想要:
row_indices = find([1,1,1, A(4:end,3).'<600])
前 3 行是因为您总是想要前 3 行。
因此,将所有内容放在一起就可以
在示例值上运行此命令:
顺便说一句,将这些行更改为 1 也同样简单:
Firstly your problem is that entering a matrix inside a matrix means use the values of the matrix as indices. since your matrix only has zeros and ones (as a result of the "<" operator) you got a weird result.
To answer your question, you need to replace the matrix with a smaller matrix. something like:
A = new_matrix
new_matrix is generated out of A by taking only the rows you want.
new_matrix = A(row_indices, :)
Where row_indices is the vector of indices you want to keep. To build that you can start with a zeros-ones vector and apply find (which will yield the indices with ones). So we want:
row_indices = find([1,1,1, A(4:end,3).'<600])
The first 3 ones are because you always want the first 3 lines.
So putting everything together gives
Running this on your example values :
BTW, changing these rows to ones is just as easy: