1
votes

I want to convert a vector of integers into a logical matrix. This is how I want to do it. Let's say the vector is of size M, and the maximum element is A. The output should be a logical matrix of size MxA, where each row with value v_i is 0 from column 1 to (v_i - 1), and is 1 from column v_i to A. Here's an example:

3
4
4
1

The output should be:

0 0 1 1
0 0 0 1
1 1 1 1
1 1 1 1

I can do this quite easily with a loop but is there a loop-less way to do it in MATLAB?

1

1 Answers

3
votes

Easy. First define your data:

vector = [3; 4; 4; 1];
M = length(vector);
A = 4;

Then (using bsxfun):

output = bsxfun(@ge, 1:A, vector(:));

Alternatively (with repmat):

output = repmat(1:A,M,1) >= repmat(vector(:),1,A);