How to count patterns in matrix rows?

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.

 Respuesta aceptada

Sean de Wolski
Sean de Wolski el 19 de Dic. de 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 comentarios

Michael
Michael el 19 de Dic. de 2011
Hi Sean -- thanks for the quick answer. Is there a way to make this work for partial rows? What I mean is that I only care about the values of a few of the columns at a time and the others can have any value. In the example above, what if I wanted to check only column 2 and column 4 to have the values zero and one, respectively?
Thanks so much for your time.
Sean de Wolski
Sean de Wolski el 19 de Dic. de 2011
sure. Break the call into multiple lines and extract only the columns you care about. For columns 2,4 from above
Asub = A(:,[2 4])
sum(all(bsxfun(@eq,Asub,[0 0])))
Sean de Wolski
Sean de Wolski el 19 de Dic. de 2011
And for only checking zeros (or other scalar values across all columns) you have it even easier, no bsxfun needed)
sum(all(Asub==0,2))
Michael
Michael el 19 de Dic. de 2011
Thank you Sean.
Michael
Michael el 19 de Dic. de 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 el 19 de Dic. de 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."

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by