简体   繁体   English

如何在octave / matlab中找到多维数组中具有相同值的所有单元格

[英]How can I find all the cells that have the same values in a multi-dimensional array in octave / matlab

How can I find all the cells that have the same values in a multi-dimensional array? 如何在多维数组中找到具有相同值的所有单元格?

I can get it partially to work with result=A(:,:,1)==A(:,:,2) but I'm not sure how to also include A(:,:,3) I tried result=A(:,:,1)==A(:,:,2)==A(:,:,3) but the results come back as all 0 when there should be 1 correct answer which is where the number 8 is located in the same cell on all the pages of the array. 我可以部分地使用result = A(:,:,1)== A(:,:,2),但我不知道如何也包括A(:,:,3)我试过结果= A(:,:,1)== A(:,:,2)== A(:,:,3)但是当应该有1个正确答案时,结果将返回全0,其中数字8是位于阵列所有页面上的同一单元格中。 Note: this is just a test the repeating number could be found multiple times and as different numbers. 注意:这只是一个测试,可以多次找到重复的数字和不同的数字。

PS: I'm using octave 3.8.1 which is like matlab PS:我使用octave 3.8.1,就像matlab一样

See code below: 见下面的代码:

clear all, tic
%graphics_toolkit gnuplot %use this for now it's older but allows zoom
A(:,:,1)=[1 2 3; 4 5 6; 7 9 8]; A(:,:,2)=[9 1 7; 6 5 4; 7 2 8]; A(:,:,3)=[2 4 6; 8 9 1; 3 5 8] 
[i j k]=size(A)
for ii=1:k
    maxamp(ii)=max(max(A(:,:,ii)))
    Ainv(:,:,ii)=abs(A(:,:,ii)-maxamp(ii));%the extra max will get the max value of all values in array
end

%result=A(:,:,1)==A(:,:,2)==A(:,:,3)
result=A(:,:,1)==A(:,:,2)
result=double(result); %turns  logical index into double to do find
[row col page] = find(result) %gives me the col, row, page

This is the output it gives me: 这是它给我的输出:

>>>A =

ans(:,:,1) =

   1   2   3
   4   5   6
   7   9   8

ans(:,:,2) =

   9   1   7
   6   5   4
   7   2   8

ans(:,:,3) =

   2   4   6
   8   9   1
   3   5   8

i =  3
j =  3
k =  3
maxamp =  9
maxamp =

   9   9

maxamp =

   9   9   9

result =

   0   0   0
   0   1   0
   1   0   1

row =

   3
   2
   3

col =

   1
   2
   3

page =

   1
   1
   1

Use bsxfun ( MATLAB doc , Octave doc ) and check to see if broadcasting the first slice is equal across all slices with a call to all ( MATLAB doc , Octave doc ): 使用bsxfunMATLAB文档倍频DOC ),并检查是否广播第一片是跨越与一个呼叫所有片等于allMATLAB文档八度DOC ):

B = bsxfun(@eq, A, A(:,:,1));
result = all(B, 3);

If we're playing code golf , a one liner could be: 如果我们正在玩代码高尔夫 ,那么一个衬垫可能是:

result = all(bsxfun(@eq, A, A(:,:,1)), 3);

The beauty of the above approach is that you can have as many slices as you want in the third dimension, other than just three. 上述方法的优点在于,您可以在第三维中拥有任意数量的切片,而不仅仅是三个切片。

Example

%// Your data
A(:,:,1)=[1 2 3; 4 5 6; 7 9 8]; 
A(:,:,2)=[9 1 7; 6 5 4; 7 2 8]; 
A(:,:,3)=[2 4 6; 8 9 1; 3 5 8];

B = bsxfun(@eq, A, A(:,:,1));
result = all(B, 3);

... gives us: ... 给我们:

>> result

result =

     0     0     0
     0     0     0
     0     0     1

The above makes sense since the third row and third column for all slices is the only value where every slice shares this same value (ie 8). 以上是有意义的,因为所有切片的第三行和第三列是每个切片共享该相同值(即8)的唯一值。

这是另一种方法:计算沿第三维的差异并检测所有这些差异何时为零:

result = ~any(diff(A,[],3),3);

你可以做

result = A(:,:,1) == A(:,:,2) & A(:,:,1) == A(:,:,3);

sum the elements along the third dimension and divide it with the number of dimensions. 对第三维的元素sum ,并将其除以维数。 We get back the original value if the values are the same in all dimension. 如果值在所有维度中相同,则返回原始值。 Otherwise a different (eg a decimal) value. 否则是一个不同的(例如十进制)值。 Then find the location where A and the summation are equal over the third dimension. 然后找到A和求和在第三维上相等的位置。

all( A == sum(A,3)./size(A,3),3)

ans =

0   0   0
0   0   0
0   0   1

or 要么

You could also do 你也可以这样做

all(A==repmat(sum(A,3)./size(A,3),[1 1 size(A,3)]),3)

where repmat(sum(A,3)./size(A,3),[1 1 size(A,3)]) would highlight the implicit broadcasting of this when compared with A . 其中repmat(sum(A,3)./size(A,3),[1 1 size(A,3)])将时相比突出的这个隐式广播A

or 要么

you skip the broadcasting altogether and just compare it with the first slice of A 你完全跳过广播,只是将它与第一片A进行比较

A(:,:,1) == sum(A,3)./size(A,3)

Explanation 说明

3 represents the third dimension . 3表示第三维。 sum(A,3) means that we are taking the sum over the third dimension. sum(A,3)意味着我们将总和超过第三维。 Then we divide that sum by the number of dimensions. 然后我们将这个总和除以维数。 It's basically the average value for that position in the third dimension. 它基本上是第三维中该位置的平均值。 If you add three values and then divide it by three then you get the original value back. 如果添加三个值然后除以三,则返回原始值。 For example, A(3,3,:) is [8 8 8] . 例如, A(3,3,:)[8 8 8] (8+8+8)/3 = 8 . (8+8+8)/3 = 8 If you take another example, ie the value above, A(2,3,:) = [6 4 1] . 如果你拿另一个例子,即上面的值, A(2,3,:) = [6 4 1] Then (6+4+1)/3=3.667 . 然后(6+4+1)/3=3.667 This is not equal to A(2,3,:) . 这不等于A(2,3,:)

sum(A,3)./size(A,3)
ans =

   4.0000   2.3333   5.3333
   6.0000   6.3333   3.6667
   5.6667   5.3333   8.0000

Therefore, we know that the elements are not the same throughout the third dimension. 因此,我们知道在整个第三维中元素并不相同。 This is just a trick I use to determine that. 这只是我用来确定的一个技巧。 You also have to remember that sum(A,3)./size(A,3) is originally a 3x3x1 matrix that will be automatically expanded (ie broadcasted) to a 3x3x3 matrix when we do the comparison with A ( A == sum(A,3)./size(A,3) ). 您还必须记住, sum(A,3)./size(A,3)最初是一个3x3x1矩阵,当我们与A进行比较时,它将自动扩展(即广播)到3x3x3矩阵( A == sum(A,3)./size(A,3) )。 The result of that comparison will be a logical array with 1 for the positions that are the same throughout the third dimension. 该比较的结果将是一个逻辑数组,其中1表示在整个第三维中相同的位置。

A == sum(A,3)./size(A,3)
ans =

ans(:,:,1) =

   0   0   0
   0   0   0
   0   0   1

ans(:,:,2) =

   0   0   0
   1   0   0
   0   0   1

ans(:,:,3) =

   0   0   0
   0   0   0
   0   0   1

Then use all(....,3) to get those. 然后使用all(....,3)来获取那些。 The result is a 3x3x1 matrix where a 1 indicates that the value is the same in the third dimension. 结果是3x3x1矩阵,其中1表示该值在第三维中相同。

all( A == sum(A,3)./size(A,3),3)
ans =

   0   0   0
   0   0   0
   0   0   1

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

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