First, read your jpeg image into a Matlab 3D array (first two dimensions indicate position, third dimension indicates R,G,B):
X = imread('image.jpg');
Then:
index = find(X(:,:,1)<=31 & X(:,:,2)<=31 & X(:,:,3)<=31);
R = X(index);
G = X(index + size(X,1)*size(X,2));
B = X(index + 2*size(X,1)*size(X,2));
does what you want. It uses the concept of linear indexing.