Suppose i have a 1000*24 binary vectors each having 4 ones and 20 zeros in each row .Now want to extract the spcific vectors that have ones only at the speific positions.

For example
I have all stored all these vectors in a matrix
AA=[ 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 1 0 0 0 0
0 0 0 0 1 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0
0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 0
0 1 1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 0
0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0
0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 1 0
0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0];
Now i want only those vectors and their indexes (of vectors) that have ones at the positions 12,13,14,15 and taht have ones at the positions 14,15,16 and 17 and want them to be stored separtely in another matrix.

 Accepted Answer

Is that two separate cases of 12:15 AND 14:17 or one case that includes both cases? And is it all or any that must be 1?
V1=12:15; % the first search vector
ix1=all(A(:,V1),2); % the logical vector if it is "all"
M1=A(ix1,:); % the subset array
I1=find(ix1); % the numeric indices of M1 rows in the original A
Shouldn't be hard to generalize; if the need is for any one element instead of all, then simply replace all with any above.

6 Comments

Weil, you'll have to show what that would be; you didn't answer the Q? about what your initial Q? meant, specifically nor describe what isn't what expected in one or the other of the two possible interpretations above...we only know what is given.
The example could be much smaller and so is more easily visualized and still serve the purpose as well...
nCr=10626;
n = 24; % total number of elements
p=[10 8.1 8.2 12 9 12.1 20.1 26.2 27 17 16.1 16 15.9 14 8.2 8 8.1 9 8 8.05 8.01 8.02 8.02 8.09];
Pr_kw=500/1000;
No_ones = 4; % number of ones
v = zeros(n,1);
v(1:No_ones,1) = 1; % vector with desired entries
AA=[];
for i=1:nCr;
Popp=reshape(v(randperm(n)),1,24);
AA=[AA; Popp];
end
Dear Sir,
When we run we get Popp matrix that have 10626 rows and 24 columns now i want only vectors that are like
=[0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0]
The vectors that have ones at the positions 12 13 14 15 16 17 (ones that occur 4 times at either of these positions and the indices of that vector too.
Well, that's only a small modification to the preceding to count the number of matches...
V1=12:17; % the first search vector
N=4; % the number to match in array subset V1
ix1=sum(A(:,V1),2)==N; % the logical vector if it is "all"
M1=A(ix1,:); % the subset array
I1=find(ix1); % the numeric indices of M1 rows in the original A
Thank you so much for your quick response.
it helped alot thanks again.
Glad to help -- when know what the problem is, it's easier... <VBG>
So if this solved the problem or gave you the tools with which to answer, go ahead and Accept the Answer. Let's others know resolution has been reached if nothing else...

Sign in to comment.

More Answers (0)

Asked:

on 7 Oct 2022

Commented:

dpb
on 12 Oct 2022

Community Treasure Hunt

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

Start Hunting!