Generate a matrix of 1 and 0

I want to generate a matrix B of 1's and 0's corresponding to a vector A of Integers.
For Example if A =[4 5 3 2 4 2 1 4]; is my allocation vector which represents that thing 1,5,8 are assigned to user 4, 2 is assigned to user 5, 3 to user 3, 4, 6 to user 2 and 7 to user 1.
Now i want to generate a Matrix which will have users as columns and thing assigned as rows (or opposite), if a thing is assigned to that user it will have an entry 1 that particular row if it is not assigned that it will have a entry zero in that row. Thus B user,thing =1 or 0. that is B4,1 B4,5 B4,8 are 1 and B4,2 B4,3 B4,4 B4,6 B4,7 are zero. How to do this in MATLAB

1 Comment

Your explanation is very hard to follow. Please show a simple example that has BOTH the input AND the output.
A = [4 5 3 2 4 2 1 4];
B = ??? SHOW THIS

Sign in to comment.

 Accepted Answer

Matt Fig
Matt Fig on 23 Aug 2012
Edited: Matt Fig on 23 Aug 2012
Is this what you mean:
A =[4 5 3 2 4 2 1 4];
U = unique(A);
B = zeros(max(U));
for ii = U
B(A==ii,ii) = 1;
end
.
.
.
EDIT
If so, then this is a vectorized version:
[D,I] = sort(A);
A =[4 5 3 2 4 2 1 4];
B = zeros([max(I),max(D)]);
B(sub2ind(size(B),I,D)) = 1;

2 Comments

Thanks a ton I was looking for exactly this answer
I would like to extend this question further by asking for some more help; Its basically an allocation problem I have a Matrix of Gains for each user (N)on each channel (K) which is NxK matrix. based on this matrix and to maximize through put I will allocate the channel with best gain for a user to that user..most likely one user will get more than one channel as K>N. But some users(SAY N3)whose channel gain is not so good on any of the channels may not get any channel, since I wanted to maximize the throughput. But I want to include some fairness of allocation by finding out a user (N7) with most numbers of channels allocated and borrowing some of the channels with best gain for the user (N3) with zero channels allocated and give these channels to to User (N3). Further I want to include proportional fairness between users say the ration of throughput of individual users should be 1:2:4. Please help. Channel Gain matrix is random. And throughput will be product of Gain*power(which is assumed to be same for all users on all channels), summed over all channels allocated to that user, summed over all users.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 23 Aug 2012
Edited: Image Analyst on 23 Aug 2012
How about
B = A==4
Gives:
B =
1 0 0 0 1 0 0 1
or
B = find(A==4);
Gives:
B =
1 5 8

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Asked:

on 23 Aug 2012

Community Treasure Hunt

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

Start Hunting!