Link to home
Start Free TrialLog in
Avatar of Member_2_2394978
Member_2_2394978Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Iterate through binary vector

Hi,

I've got a binary vector N long and I want to be able to loop through all possible values of the vector and do some processing on it.
For example if N = 3;

001
Process
010
Process
100
Process
101
Process
011
Process
110
Process
111
Process

But would like to be able to do this in some sort of loop construction with N. So can then scale up to bigger sized vectors!

Any suggestions?
James
Avatar of Frosty555
Frosty555
Flag of Canada image

I feel that a good direction to go with this is to instead use an N-dimensional matrix, instead of a single column vector filled with values. Fill each dimension of the matrix with alternating zeros and ones and then loop through each entry in the matrix in the correct way to iterate every possibility.

It would be easy to fill every alternative entry. To do it with a simple vector you can do something like:

x = zeros(50,1);
x(1:2:length(x)) = x(1:2:length(x)) + 1;

But I'm shaky on my matlab and I don't know if this is really the right way to do it. Some food for thought though.
Avatar of Member_2_2394978

ASKER

Hi, thanks for a quick response.
Not quite sure I understand what your suggesting.
Can you clarify?

James
Ah nevermind that's a stupid way of doing it. You only need a two dimensional matrix. What what I babbling about before?

You'll need to define a n by 2^n matrix using the zeros operator:

x = zeros(2^n, n);

Now, each row is a potential vector for you to analyze.

You need to fill in the columns following this pattern (for a 4-bit example)

0000
1000
0100
1100
0010
1010
0110
1110
0001
1001
0101
1101
0011
1011
0111
1111

Notice how the first column is just 0, 1, 0, 1. The second column is 0, 0, 1, 1, 0, 0, 1, 1. The third column is 0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1 etc. The number of consecutive numbers multiplies by two every time. What does this look like? It looks to me just like a square wave, or a clock signal.

The first column is just a clock signal with a period of 2
The second column is a clock signal with a period of 4
The third column is a clock signal with a period of 8
... etc.

You can also, conveniently, use matrix operations in MatLab to set the value of an entire column in one fell swoop, e.g.

x(:, 1) = [a clock signal with period 2]
x(:, 2) = [a clock signal with period 4]
x(:, 3) = [a clock signal with period 8]

etc.

All we need to do now is figure out how to synthesize that "clock signal" in matlab, which SURELY must be an easy thing to do. In fact, there's even a function called "square()" that is ALMOST what you want:

http://www.mathworks.com/access/helpdesk/help/toolbox/signal/index.html?/access/helpdesk/help/toolbox/signal/square.html

But it needs some tweaking to get anything other than a period of 2 to work. I'll keep hunting but this should put you on the right track. If you implement it this way, your function will not only be effective, and short, but also blazingly fast since you used almost entirely matrix operations to do it.
Absolutely wonderful, I shall implement this when I have some time and have a look at the link :)

Thanks
Now, the square function isn't quite right. It just generates 0,1,0,1,0,1. The period is 2. Always.

You'll run into trouble generating clock cycles with periods other than 2, but I'm sure there's some trickery you can do to puff out a matrix that looks like [0,1,0,1] into something like [0,0,0,1,1,1,0,0,0,1,1,1].
ASKER CERTIFIED SOLUTION
Avatar of Frosty555
Frosty555
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I propose a solution using binary representation of integers. It works up to N=54, what will be 2^54-1 ~1.8e16 possible vectors.
You can get all the vectors with dec2bin(1:2^N-1)-48 (easy to get out of memory). 48 is ascii code for 0.

N=3;
for i=1:2^N-1
    dec2bin(i,N)-48
end

Open in new window

Avatar of WillDC
WillDC

To create a matrix M of size R by C, with R indexed from zero to exp(2,C)-1, and which includes all possible binary vectors of size C, you can set M[r,c] equal to zero if ((r mod period(c)) < period(c)/2) evaluates to TRUE, and 1 otherwise. Period(c) is just 2 for the first column, 4 for the second, 8 for the third, 16, etc.