【发布时间】:2021-07-17 18:14:04
【问题描述】:
我有一个向量。假设 x=[0 -2 -1 -1 -1 0 0 -1]。我想从 x 向量中找到符号。符号为 (1,-2), (0,-1), (0,-1), (0,-1), (2,-1)。
(1,-2) 表示“-2”前面有一个“0”。 (0,-1) 表示“-1”前面没有“0”。 (2,-1) 表示“-1”前面有两个“0”。
有什么想法吗?编码好像有点难。
【问题讨论】:
标签: matlab vector statistics
我有一个向量。假设 x=[0 -2 -1 -1 -1 0 0 -1]。我想从 x 向量中找到符号。符号为 (1,-2), (0,-1), (0,-1), (0,-1), (2,-1)。
(1,-2) 表示“-2”前面有一个“0”。 (0,-1) 表示“-1”前面没有“0”。 (2,-1) 表示“-1”前面有两个“0”。
有什么想法吗?编码好像有点难。
【问题讨论】:
标签: matlab vector statistics
您可以使用find 和diff 做到这一点:
x=[0 -2 -1 -1 -1 0 0 -1];
idx = find(x ~= 0); % Get the positions of the non-zero elements
symbols = [diff([0,idx])-1; x(idx)]; % Number of positions since previous non-zero
% With the corresponding element underneath
获取输出
symbols =
1 0 0 0 2
-2 -1 -1 -1 -1
你的配对对应于这个数组中的列。
【讨论】: