How to count patterns in matrix rows?

1 次查看(过去 30 天)
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.

采纳的回答

Sean de Wolski
Sean de Wolski 2011-12-19
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:
  • compare elements in each row to see if they're equal.
  • all elements in each row equal
  • sum them
  6 个评论
Michael
Michael 2011-12-19
above in the comments when you said:
sum(all(bsxfun(@eq,Asub,[0 0])))
you meant:
sum(all(bsxfun(@eq,Asub,[0 0]),2))
right?
Sean de Wolski
Sean de Wolski 2011-12-19
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."

请先登录,再进行评论。

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by