I have a vector for example
我有一个向量。
a = [0 1 0 3]
I want to turn a into b which equals b = [1 3]
.
我想把a变成b = b =[1 3]。
How do I perform this in general? So I have a vector with some zero components and I want to remove the zeroes and leave just the non-zero numbers?
我一般怎么做这个?所以我有一个零分量的向量我想把0去掉,只留下非零的数?
7 个解决方案
#1
79
If you just wish to remove the zeros, leaving the non-zeros behind in a, then the very best solution is
如果你只是想去掉零,把非零放在a后面,那么最好的解就是。
a(a==0) = [];
This deletes the zero elements, using a logical indexing approach in MATLAB. When the index to a vector is a boolean vector of the same length as the vector, then MATLAB can use that boolean result to index it with. So this is equivalent to
这将删除零元素,在MATLAB中使用逻辑索引方法。当向量的索引是与向量相同长度的布尔向量时,MATLAB可以使用这个布尔结果来对它进行索引。所以这是等价的。
a(find(a==0)) = [];
And, when you set some array elements to [] in MATLAB, the convention is to delete them.
并且,当你在MATLAB中设置一些数组元素时,惯例是删除它们。
If you want to put the zeros into a new result b, while leaving a unchanged, the best way is probably
如果你想把0代入一个新的结果b,而保持不变,最好的方法可能是。
b = a(a ~= 0);
Again, logical indexing is used here. You could have used the equivalent version (in terms of the result) of
这里再次使用了逻辑索引。您可以使用等效版本(在结果方面)。
b = a(find(a ~= 0));
but mlint will end up flagging the line as one where the purely logical index was more efficient, and thus more appropriate.
但mlint最终会将这条线标记为纯逻辑索引更有效,因此更合适。
As always, beware EXACT tests for zero or for any number, if you would have accepted elements of a that were within some epsilonic tolerance of zero. Do those tests like this
和往常一样,要注意对零或任何数字的精确测试,如果你愿意接受一个在某些epsilonic容忍度为0的元素。像这样的测试吗?
b = a(abs(a) >= tol);
This retains only those elements of a that are at least as large as your tolerance.
这只保留了至少和你的容忍度一样大的元素。
#2
8
I just came across this problem and wanted to find something about the performance, but I couldn't, so I wrote a benchmarking script on my own:
我刚刚遇到了这个问题,想要找到一些关于性能的东西,但是我不能,所以我自己写了一个基准测试脚本:
% Config:
rows = 1e6;
runs = 50;
% Start:
orig = round(rand(rows, 1));
t1 = 0;
for i = 1:runs
A = orig;
tic
A(A == 0) = [];
t1 = t1 + toc;
end
t1 = t1 / runs;
t2 = 0;
for i = 1:runs
A = orig;
tic
A = A(A ~= 0);
t2 = t2 + toc;
end
t2 = t2 / runs;
t1
t2
t1 / t2
So you see, the solution using A = A(A ~= 0)
is the quicker of the two :)
你看,A = A(A ~= 0)的解是2的更快
#3
4
I often ended up doing things like this. Therefore I tried to write a simple function that 'snips' out the unwanted elements in an easy way. This turns matlab logic a bit upside down, but looks good:
我经常做这样的事情。因此,我尝试写一个简单的函数,用简单的方法“剪掉”不想要的元素。这使matlab逻辑有点颠倒,但看起来不错:
b = snip(a,'0')
you can find the function file at: http://www.mathworks.co.uk/matlabcentral/fileexchange/41941-snip-m-snip-elements-out-of-vectorsmatrices
你可以在http://www.mathworks.co.uk/matlabcentral/fileexchange/41941-snip-m-snip- elementsmatrix中找到这个函数文件。
It also works with all other 'x', nan or whatever elements.
它也适用于所有其他的x, nan或其他元素。
#4
3
b = a(find(a~=0))
#5
2
Why not just, a=a(~~a)
or a(~a)=[]
. It's equivalent to the other approaches but certainly less key strokes.
为什么不只是a=a(~~a)或a(~a)=[]。它等价于其他的方法,但肯定不是重点。
#6
2
Data
数据
a=[0 3 0 0 7 10 3 0 1 0 7 7 1 7 4]
Do
做
aa=nonzeros(a)'
Result
结果
aa=[3 7 10 3 1 7 7 1 7 4]
#7
2
You could use sparse(a), which would return
您可以使用稀疏(a),它将返回。
(1,2) 1
(1、2)1
(1,4) 3
(1、4)3
This allows you to keep the information about where your non-zero entries used to be.
这允许您保留关于非零条目的位置信息。
#1
79
If you just wish to remove the zeros, leaving the non-zeros behind in a, then the very best solution is
如果你只是想去掉零,把非零放在a后面,那么最好的解就是。
a(a==0) = [];
This deletes the zero elements, using a logical indexing approach in MATLAB. When the index to a vector is a boolean vector of the same length as the vector, then MATLAB can use that boolean result to index it with. So this is equivalent to
这将删除零元素,在MATLAB中使用逻辑索引方法。当向量的索引是与向量相同长度的布尔向量时,MATLAB可以使用这个布尔结果来对它进行索引。所以这是等价的。
a(find(a==0)) = [];
And, when you set some array elements to [] in MATLAB, the convention is to delete them.
并且,当你在MATLAB中设置一些数组元素时,惯例是删除它们。
If you want to put the zeros into a new result b, while leaving a unchanged, the best way is probably
如果你想把0代入一个新的结果b,而保持不变,最好的方法可能是。
b = a(a ~= 0);
Again, logical indexing is used here. You could have used the equivalent version (in terms of the result) of
这里再次使用了逻辑索引。您可以使用等效版本(在结果方面)。
b = a(find(a ~= 0));
but mlint will end up flagging the line as one where the purely logical index was more efficient, and thus more appropriate.
但mlint最终会将这条线标记为纯逻辑索引更有效,因此更合适。
As always, beware EXACT tests for zero or for any number, if you would have accepted elements of a that were within some epsilonic tolerance of zero. Do those tests like this
和往常一样,要注意对零或任何数字的精确测试,如果你愿意接受一个在某些epsilonic容忍度为0的元素。像这样的测试吗?
b = a(abs(a) >= tol);
This retains only those elements of a that are at least as large as your tolerance.
这只保留了至少和你的容忍度一样大的元素。
#2
8
I just came across this problem and wanted to find something about the performance, but I couldn't, so I wrote a benchmarking script on my own:
我刚刚遇到了这个问题,想要找到一些关于性能的东西,但是我不能,所以我自己写了一个基准测试脚本:
% Config:
rows = 1e6;
runs = 50;
% Start:
orig = round(rand(rows, 1));
t1 = 0;
for i = 1:runs
A = orig;
tic
A(A == 0) = [];
t1 = t1 + toc;
end
t1 = t1 / runs;
t2 = 0;
for i = 1:runs
A = orig;
tic
A = A(A ~= 0);
t2 = t2 + toc;
end
t2 = t2 / runs;
t1
t2
t1 / t2
So you see, the solution using A = A(A ~= 0)
is the quicker of the two :)
你看,A = A(A ~= 0)的解是2的更快
#3
4
I often ended up doing things like this. Therefore I tried to write a simple function that 'snips' out the unwanted elements in an easy way. This turns matlab logic a bit upside down, but looks good:
我经常做这样的事情。因此,我尝试写一个简单的函数,用简单的方法“剪掉”不想要的元素。这使matlab逻辑有点颠倒,但看起来不错:
b = snip(a,'0')
you can find the function file at: http://www.mathworks.co.uk/matlabcentral/fileexchange/41941-snip-m-snip-elements-out-of-vectorsmatrices
你可以在http://www.mathworks.co.uk/matlabcentral/fileexchange/41941-snip-m-snip- elementsmatrix中找到这个函数文件。
It also works with all other 'x', nan or whatever elements.
它也适用于所有其他的x, nan或其他元素。
#4
3
b = a(find(a~=0))
#5
2
Why not just, a=a(~~a)
or a(~a)=[]
. It's equivalent to the other approaches but certainly less key strokes.
为什么不只是a=a(~~a)或a(~a)=[]。它等价于其他的方法,但肯定不是重点。
#6
2
Data
数据
a=[0 3 0 0 7 10 3 0 1 0 7 7 1 7 4]
Do
做
aa=nonzeros(a)'
Result
结果
aa=[3 7 10 3 1 7 7 1 7 4]
#7
2
You could use sparse(a), which would return
您可以使用稀疏(a),它将返回。
(1,2) 1
(1、2)1
(1,4) 3
(1、4)3
This allows you to keep the information about where your non-zero entries used to be.
这允许您保留关于非零条目的位置信息。