[英]MATLAB - Avoid repeated values in a vector inside cell arrays and take next
This is the problem: 这就是问题:
I have a cell array on the form indx{ii}
where each ii
is an array of size 1xNii
(this means the arrays have different size). 我在
indx{ii}
形式上有一个单元格数组,其中每个ii
是大小为1xNii
的数组(这意味着数组具有不同的大小)。 And another cell array on the form indy{jj}
where each jj
is an array of the same size as ii
. 还有
indy{jj}
形式的另一个单元格数组,其中每个jj
是与ii
大小相同的数组。
The question is that I would like to create a function evaluates the values in the arrays of indx{:}
and take the first one that is not repeated, and if is a repeated value then take the next. 问题是我想创建一个函数来计算
indx{:}
数组中的值,并采用第一个不重复的值,如果是重复值,则采用下一个值。
I will try to explain with an example. 我将尝试用一个例子来解释。 Suppose we have
indx
and indy
that are the cell arrays: 假设我们有
indx
和indy
这两个单元格数组:
indx{1} = [1 3 2 7];
indx{2} = [3 8 5];
indx{3} = [3 6 2 9];
indx{4} = [1 3 4];
indx{5} = [3 1 4];
indy{1} = [0.12 0.21 0.31 0.44];
indy{2} = [0.22 0.34 0.54];
indy{3} = [0.13 0.23 0.36 0.41];
indy{4} = [0.12 0.16 0.22];
indy{5} = [0.14 0.19 0.26];
What I want the code to do is take the first value and is not repeated in indx
and the equivalent in indy
. 我要代码执行的操作是获取第一个值,并且在
indx
不重复,而在indy
等效。 So the answer for the example should be: 因此,示例的答案应为:
ans=
indx{1} = 1;
indx{2} = 3;
indx{3} = 6;
indx{4} = 4;
indx{5} = [];
indy{1} = 0.12;
indy{2} = 0.22;
indy{3} = 0.23;
indy{4} = 0.22;
indy{5} = [];
In ans
, for indx{1}
the code takes 1
because is the first and it's not repeated and takes the equivalent value in indy
. 在
ans
,对于indx{1}
,代码采用1
因为它是第一个,并且不重复,并且在indy
采用等效值。 Then for indx{2}
it takes 3
because is the first value and is not repeated as first value in any array before. 然后,对于
indx{2}
它需要3
因为它是第一个值,并且之前没有在任何数组中重复作为第一个值。 But for ind{3}
it takes 6
, because the first value that is 3
is repeated, and takes the equivalent value to 6
in indy
which is 0.23
. 但是对于
ind{3}
它取6
,因为重复的第一个值是3
,并且将indy
的等效值取为6
,即0.23
。 For ind{4}
the first and second value they are already repeated as first values so the code takes 4
and its equivalent in indy
. 对于
ind{4}
的第一个和第二个值,它们已经作为第一个值重复了,因此代码取4
和indy
等效值。 And last, for indx{5}
since all values are already repeated the code should take no value. 最后,对于
indx{5}
由于所有值均已重复,因此代码不应取值。
indx{1} = [1 3 2 7];
indx{2} = [3 8 5];
indx{3} = [3 6 2 9];
indx{4} = [1 3 4];
indx{5} = [3 1 4];
indy{1} = [0.12 0.21 0.31 0.44];
indy{2} = [0.22 0.34 0.54];
indy{3} = [0.13 0.23 0.36 0.41];
indy{4} = [0.12 0.16 0.22];
indy{5} = [0.14 0.19 0.26];
indx2 = NaN(numel(indx),1);
indx2(1) = indx{1}(1);
indy2 = NaN(numel(indy),1);
indy2(1) = indy{1}(1);
for ii = 2:numel(indx)
tmp1 = indx{ii}'; % get the original as array
tmp2 = indy{ii}';
if numel(tmp1)>numel(indx2)
tmp3 = [indx2;NaN(numel(tmp1)-numel(indx2),1)];
tmp4 = [indx2;NaN(numel(tmp1)-numel(indx2),1)];
else
tmp1 = [tmp1;NaN(numel(indx2)-numel(tmp1),1)];
tmp2 = [tmp2;NaN(numel(indx2)-numel(tmp2),1)];
tmp3 = indx2;
tmp4 = indy2;
end
tmp5 = ~ismember(tmp1,tmp3); % find first non equal one
tmp6 = find(tmp5,1,'first');
indx2(ii) = tmp1(tmp6); % save values
indy2(ii) = tmp2(tmp6);
end
N = numel(indx2);
indx2 = mat2cell(indx2, repmat(1,N,1));
N = numel(indy2);
indy2 = mat2cell(indy2, repmat(1,N,1));
indx2 =
[ 1]
[ 3]
[ 6]
[ 4]
[NaN]
What I have done here is to first initialise your output cells to have the same number of cells as your original data. 我在这里要做的是首先将输出单元格初始化为具有与原始数据相同数量的单元格。 Then I assign value 1, since that one will always be unique, it is the first entry.
然后,我分配值1,因为该值将始终是唯一的,因此它是第一项。 After that I use a
for
loop to first convert all four cell arrays (2 input, two output) to regular arrays for processing with ismember
, where I check for the all non-equal number between the next input cell and the existing numbers in your output. 之后,我使用
for
循环首先将所有四个单元格数组(2个输入,两个输出)转换为常规数组,以使用ismember
进行处理,在其中检查下一个输入单元格与您的现有数字之间的所有非等号输出。 Then find
is employed to get the first non-matching number. 然后使用
find
获得第一个不匹配的数字。 Lastly, the numbers are assigned to the arrays if present. 最后,将数字分配给数组(如果存在)。
As a comment on the usage of booleans with NaN
, try NaN ~=NaN
and NaN ==NaN
. 作为对
NaN
使用布尔值的评论,请尝试NaN ~=NaN
和NaN ==NaN
。 The first will give you 1, whilst the second will give you zero. 第一个给你1,而第二个给你零。 This quality makes NaNs the ideal choice of filler here, because
0 == 0
will result in 1: 这种质量使NaN在这里成为填充剂的理想选择,因为
0 == 0
将导致1:
A = [1,2,5,4,NaN];
B = [1,3,7,NaN,NaN];
ismember(A,B)
=
1 0 0 0 0
Thus the NaNs do not equal one another and will therefore not pollute your solution. 因此,NaN彼此不相等,因此不会污染您的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.