- compare elements in each row to see if they're equal.
- all elements in each row equal
- sum them
How to count patterns in matrix rows?
1 view (last 30 days)
Show older comments
I have some large, ternary matrices encoding sum-of-products representations of logic functions -- so they have zeros, ones and don't-cares, which I represent with -1. Each row is a term, and each column is a variable. These details may not matter but they may help explain what I'm asking.
I want to ask a question like, "how many times are x2 and x4 set to (0, 0) across all the terms in this function". This translates to MATLAB in the form, "how many rows in this matrix have a zero in column two and a zero in column four?"
This question will start with pairs and extend to triplets, and maybe even four or five columns (variables) at a time. Furthermore I'll want to enumerate the 2^n binary combinations of these variables.
Just to give an idea of scale, I'm currently working on a matrix with around 20,000 rows and 20 columns and I'd be performing the search/count described above almost 1,000 times for 2 variables (columns), but potentially tens or hundreds of thousands of times for 4 or 5 variables (columns), respectively.
Any help would be so greatly appreciated.
0 Comments
Accepted Answer
Sean de Wolski
on 19 Dec 2011
bsxfun() with eq() and all() will be your friend. Example:
A = sign(magic(5)-10); %sample data
match_me = [-1 -1 1 1 1]; %row you want
num_row_eq = sum(all(bsxfun(@eq,A,match_me),2)); %engine
Engine explained:
6 Comments
Sean de Wolski
on 19 Dec 2011
yes I did. That was the first rule of posting to ML Answers:
"Rule 1. IF you type an answer without running the code in MATLAB, you WILL make a typo."
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!