I need to find how many 0's there are in matching places, between an array and a matrix.

1 次查看(过去 30 天)
I have several [1 x 8] arrays (O1-O8) and a [12 x 8] matrix (FSM), both made up of binary digits, as follows:
FSM = [0 1 0 1 1 1 0 0 0 0 0 0; 0 1 0 1 0 0 1 1 0 0 0 0; 0 1 0 1 0 0 0 0 1 1 0 0; 1 0 0 0 0 0 0 0 0 0 0 0; 1 1 1 1 0 0 0 0 0 0 0 1; 0 1 0 1 0 0 0 1 0 0 0 0; 0 1 0 1 0 0 0 0 0 1 0 0; 0 1 0 1 0 0 0 0 0 0 1 0];
O1 = [0 0 0 0 1 1 0 0 0 0 0 0];
O2 = [0 0 0 0 0 0 1 1 0 0 0 0];
O3 = [0 0 0 0 0 0 0 0 1 1 0 0];
O4 = [1 0 0 0 0 0 0 0 0 0 0 0];
O5 = [1 1 1 1 0 0 0 0 0 0 0 0];
O6 = [0 0 1 0 0 0 0 1 0 0 0 0];
O7 = [0 0 0 0 0 0 0 0 0 1 0 0];
O8 = [0 0 0 0 0 0 0 0 0 0 1 0];
I need to find, for each array, how many times there are 0's there are in matching columns, for each row of the matrix.
For example, I would take O1, and row 1 of FSM - each of these have a 0 in the columns 1, 3, 7, 8, 9, 10, 11, 12. So they output would be 8. Then do the same for O1, for columns 2-8.
Then I can implement the code again for arrays O2-O8.

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-12-15
编辑:Dyuman Joshi 2023-12-15
FSM = [0 1 0 1 1 1 0 0 0 0 0 0; 0 1 0 1 0 0 1 1 0 0 0 0; 0 1 0 1 0 0 0 0 1 1 0 0; 1 0 0 0 0 0 0 0 0 0 0 0; 1 1 1 1 0 0 0 0 0 0 0 1; 0 1 0 1 0 0 0 1 0 0 0 0; 0 1 0 1 0 0 0 0 0 1 0 0; 0 1 0 1 0 0 0 0 0 0 1 0];
O1 = [0 0 0 0 1 1 0 0 0 0 0 0];
O2 = [0 0 0 0 0 0 1 1 0 0 0 0];
O3 = [0 0 0 0 0 0 0 0 1 1 0 0];
O4 = [1 0 0 0 0 0 0 0 0 0 0 0];
O5 = [1 1 1 1 0 0 0 0 0 0 0 0];
O6 = [0 0 1 0 0 0 0 1 0 0 0 0];
O7 = [0 0 0 0 0 0 0 0 0 1 0 0];
O8 = [0 0 0 0 0 0 0 0 0 0 1 0];
%Combine into an array, so it is easy to access the data using indexing
O = [O1;O2;O3;O4;O5;O6;O7;O8];
%Pre-allocation
out = zeros(8, 8);
%For loop to go through each data-set
for k=1:size(O,1)
out(:,k) = sum((~O(k,:)).*(~FSM), 2);
end
In the output, out(i,j) is the sum corresponding to ith row of FSM and Oj.
out
out = 8×8
8 6 6 7 6 6 7 7 6 8 6 7 6 7 7 7 6 6 8 7 6 6 8 7 9 9 9 11 8 9 10 10 5 5 5 7 7 6 6 6 7 8 7 8 7 8 8 8 7 7 8 8 7 7 9 8 7 7 7 8 7 7 8 9
Additionally, you can achieve the same result in a line of code. The for loop can be condensed into a single vectorized code -
OUT = ((~O)*(~FSM).').'
OUT = 8×8
8 6 6 7 6 6 7 7 6 8 6 7 6 7 7 7 6 6 8 7 6 6 8 7 9 9 9 11 8 9 10 10 5 5 5 7 7 6 6 6 7 8 7 8 7 8 8 8 7 7 8 8 7 7 9 8 7 7 7 8 7 7 8 9

更多回答(1 个)

atharva
atharva 2023-12-15
Hey John,
I understand that you need to determine, for each array, the frequency of occurrences of zeros in corresponding columns for every row of the matrix.
you can try the following code for O1 and then implement the code again for O2 to O8
% Given data
FSM = [0 1 0 1 1 1 0 0 0 0 0 0; 0 1 0 1 0 0 1 1 0 0 0 0; 0 1 0 1 0 0 0 0 1 1 0 0; 1 0 0 0 0 0 0 0 0 0 0 0; 1 1 1 1 0 0 0 0 0 0 0 1; 0 1 0 1 0 0 0 1 0 0 0 0; 0 1 0 1 0 0 0 0 0 1 0 0; 0 1 0 1 0 0 0 0 0 0 1 0];
O1 = [0 0 0 0 1 1 0 0 0 0 0 0];
% Initialize an array to store the counts
counts = zeros(size(FSM, 1), 1);
% Iterate over each row of FSM
for i = 1:size(FSM, 1)
% Find the indices where both O1 and the current row of FSM have 0
indices = find(O1 == 0 & FSM(i, :) == 0);
% Count the number of matching 0's and store in the counts array
counts(i) = numel(indices);
end
% Display the results
disp('Number of matching 0''s for each row of FSM:')
Number of matching 0's for each row of FSM:
disp(counts)
8 6 6 9 5 7 7 7
I hope this helps!

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by