MysteriousLab
MysteriousLab

Reputation: 394

Vector and matrix comparison in MATLAB

I have vector with 5 numbers in it, and a matrix of size 6000x20, so every row has 20 numbers. I want to count how many of the 6000 rows contain all values from the vector.

As the vector is a part of a matrix which has 80'000'000 rows, each containing unique combinations, I want a fast solution (which doesn't take more than 2 days).

Thanks

Upvotes: 0

Views: 87

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112749

With the sizes you have, a bsxfun-based approach that builds an intermediate 6000x20x5 3D-array is affordable:

v = randi(9,1,5); %// example vector
M = randi(9,6000,20); %// example matrix
t = bsxfun(@eq, M, reshape(v,1,1,[]));
result = sum(all(any(t,2),3));

Upvotes: 0

Related Questions