[英]graph representing the randomization of each column in a binary matrix
Imagine the following binary image exemplified by the matrix below. 想象下面的二进制图像,以下面的矩阵为例。 This is a simplified version of the images I'll be working with: 这是我将使用的图像的简化版本:
0 1 0 1
0 1 1 1
0 0 0 1
0 1 1 1
I want to construct a graph that will represent the randomness of each column. 我想构建一个表示每列随机性的图形。 My thought is to develop a random index = the total transitions between each value in the column / by the total possible transitions. 我的想法是开发一个随机索引=列中每个值之间的总转换/可能的总转换。 In the matrix above, each column could have a total possible of 3 transitions. 在上面的矩阵中,每列总共可能有3个转换。
For the example above: 对于上面的示例:
Column 1 would have a random index of 0% (0/3) 第1列的随机索引为0%(0/3)
Column 2 would have a random index of 66.7% (2/3) 第2栏的随机指数为66.7%(2/3)
Column 3 = 100% (3/3) 第3列= 100%(3/3)
Column 4 = 0% (0/3) even though they are 1's and not 0's. 第4列= 0%(0/3),即使它们是1而不是0。 Doesn't matter, I just want the transitions. 没关系,我只想要过渡。
Can I draw a boundary around all the 1 values and then have MATLAB sum all of the boundaries? 我可以围绕所有1个值绘制边界,然后让MATLAB对所有边界求和吗?
Let your image be defined as 让您的图像定义为
im = [ 0 1 0 1
0 1 1 1
0 0 0 1
0 1 1 1 ];
The random index you want can be computed as 您想要的随机索引可以计算为
result = sum(diff(im)~=0) / (size(im,1)-1);
Explanation: diff
computes the difference between consecutive elemtents down each column. 说明: diff
计算每列下连续元素之间的差。 The result is compared against zero ( ~=0
), and all nonzero values within each row are added (with sum
). 将结果与零进行比较( ~=0
),并将每行中的所有非零值相加(与sum
)。 Finally, the result is divided by the maximum number os transitions, which is the number of rows minus 1 ( size(im,1)-1
) 最后,将结果除以最大数量的os转换,即转换的行数减去1( size(im,1)-1
)
Equivalently, you could use xor
between consecutive rows: 同样,您可以在连续的行之间使用xor
:
result = sum(xor(im(1:end-1,:), im(2:end,:))) / (size(im,1)-1)
To calculate what you are suggesting you can just do: 要计算您的建议,您可以执行以下操作:
sum( diff(A) ~= 0 )
The diff(A)
will take the forward difference down the columns and the sum will count the number of non-zero changes. diff(A)
将沿列减去前向差,并且总和将计算非零更改的数量。 So if you do this you will get: 因此,如果您这样做,您将获得:
ans =
0 2 3 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.