I need to find how many 0's there are in matching places, between an array and a matrix.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
John
el 15 de Dic. de 2023
Comentada: Dyuman Joshi
el 15 de Dic. de 2023
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.
0 comentarios
Respuesta aceptada
Dyuman Joshi
el 15 de Dic. de 2023
Editada: Dyuman Joshi
el 15 de Dic. de 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
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).').'
2 comentarios
Más respuestas (1)
atharva
el 15 de Dic. de 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:')
disp(counts)
I hope this helps!
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!