使用find命令的Matlab错误

使用find命令的Matlab错误,matlab,vector,indexing,position,elements,Matlab,Vector,Indexing,Position,Elements,我有向量[0 0 1 0 1 1 0 1]。我想找到0和1的索引。我已尝试使用find命令,但我得到: 0x1空双列向量 我认为此代码将帮助您: >> arr = [0 0 1 1 0 1 1 0 1]; >> find(arr == 0) ans = 1 2 5 8 >> find(arr == 1) ans = 3 4 6 7 9 虽然正确返回0和1的位置,但这些索引的

我有向量
[0 0 1 0 1 1 0 1]
。我想找到0和1的索引。我已尝试使用
find
命令,但我得到:

0x1空双列向量

我认为此代码将帮助您:

>> arr = [0 0 1 1 0 1 1 0 1];
>> find(arr == 0)

ans =

     1     2     5     8

>> find(arr == 1)

ans =

     3     4     6     7     9
虽然正确返回
0
1
的位置,但这些索引的典型用例是从与这些位置匹配的另一个数组中选择元素。如果确实如此,则应依赖逻辑索引,而不是
find

tfArr = [0 0 1 1 0 1 1 0 1];
data =  reshape(magic(3),1,[]); % [8,3,4,1,5,9,6,7,2]

dataWhereOnes = data(logical(tfArr))
% equivalently to the above : data(~~tfArr)
dataWhereZeros = data(~tfArr)
其结果是:

datawhereone=
4     1     9     6     2
数据零=
8     3     5     7

请编辑问题并添加您正在使用的确切代码,在这种情况下,所需的输出是什么?你想用它做什么?我已经试过了,但是我得到了一个错误,就像问题一样。请发布你的代码和错误,或者我们无法帮助你在这里很容易找到位置,但是如果它是1000元素向量呢?没有任何区别。