简体   繁体   English

在Matlab中从矩阵中删除零

[英]Remove zeros from matrix in matlab

I have a question and I hope it is not duplicate. 我有一个问题,希望不要重复。

First, I have let say the following matrix: 首先,我要说以下矩阵:

A=[2 2 2 0 0
   1 2 3 0 0
   4 5 7 2 0]

I want to remove the zeros from A and return: 我想从A删除零并返回:

A=[2 2 2
   1 2 3
   4 5 7]

When I do 当我做

A(A==0)=[] 

I get 我懂了

A=[2 2 2 1 2 3 4 5 7]

Second, if instead of zeros I want to remove the elements that are greater than something. 其次,如果我想删除大于零的元素而不是零。 For example if I want to remove all elements greater than 6 (>6) of the following matrix B: 例如,如果我要删除以下矩阵B的所有大于6(> 6)的元素:

B=[2 2 2 5 3
   1 2 3 6 8
   4 5 7 2 1]

I get 我懂了

A=[2 2 5
   1 2 6
   4 5 2]

PS I know how to do it using loops. PS我知道如何使用循环。

First problem solution 第一个问题解决方案

A(:,find(all(A,1)))

Second problem solution 第二个问题解决方案

B(:,~any(B>6,1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM