How to count patterns in matrix rows?

2 Ansichten (letzte 30 Tage)
Michael
Michael am 19 Dez. 2011
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.

Akzeptierte Antwort

Sean de Wolski
Sean de Wolski am 19 Dez. 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:
  • compare elements in each row to see if they're equal.
  • all elements in each row equal
  • sum them
  6 Kommentare
Michael
Michael am 19 Dez. 2011
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 am 19 Dez. 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."

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by