- 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?
4 ビュー (過去 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.
0 件のコメント
採用された回答
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:
6 件のコメント
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 Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!