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.
댓글 수: 0
채택된 답변
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
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).').'
추가 답변 (1개)
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:')
disp(counts)
I hope this helps!
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!