Clear Filters
Clear Filters

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

2 views (last 30 days)
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.

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 15 Dec 2023
Edited: Dyuman Joshi on 15 Dec 2023
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

More Answers (1)

atharva
atharva on 15 Dec 2023
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!

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!