Creating a matrix from data in a vector

I have two vectors that contain positive integer values but are not consecutive. I would like to create a matrix that uses these values to place a 1 in a location. Example: I have a vector member_i = [1;1;2] and another vector member_j = [2;3;3]. I would like to create a matrix that is 2 times the number of rows of the vector and use that as the number of columns. I would also like to have the number of rows in the matrix equal 4 times the number of rows in the vectors. These vectors could be any length. I would also like to use the data from the vectors to put 1's in various locations, say for instance if member_i value is 1, the first row would have a 1 in row 1 column 1 and another 1 in row 2 column 2. Then if member_j value is 3, I would like row 3 column 5 to have a 1 and row 4 column 6 to have a 1. For the given vectors mentioned above the desired output would be:
DESIREDOUTPUT =
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
1 0 0 0 0 0
0 1 0 0 0 0
0 0 0 0 1 0
0 0 0 0 0 1
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
my current code:
Beta = zeros(4,2*member_count);
for idx5 = 1:member_count;
Beta(idx1,2*member_i(idx5,1)-1) = 1;
Beta(idx2,2*member_i(idx5,1)) = 1;
Beta(idx3,2*member_j(idx5,1)-1) = 1;
Beta(idx4,2*member_j(idx5,1)) = 1;
end
Beta =
1 0 1 0 0 0
0 1 0 1 0 0
0 0 1 0 1 0
0 0 0 1 0 1
1 0 1 0 0 0
0 1 0 1 0 0
0 0 1 0 1 0
0 0 0 1 0 1
1 0 1 0 0 0
0 1 0 1 0 0
0 0 1 0 1 0
0 0 0 1 0 1
Thanks for any help to this matlab noobie. Cedric Wannaz http://www.mathworks.com/matlabcentral/answers/contributors/1078046-cedric-wannaz was more than helpful to me before.

6 Kommentare

Matt J
Matt J am 11 Mär. 2013
I, for one, do not understand the rule by which member_i and member_j are used to fill the matrix with ones. Your example also doesn't match up with your explanation. You said there should be a 1 in row 3, column 5, but there is not.
Mark
Mark am 11 Mär. 2013
You're exactly right Matt. I goofed. That should be a 1 in row 3 column 3 and a 1 in row 4 column 4 and not columns 5 & 6 as I incorrectly stated in the question. Essentially, in this case columns 1 & 2 represent a value of 1 in either vector member_i or member_j. columns 3 & 4 represent a value of 2 in either vector member_i or member_j. columns 5 & 6 represent a value of 3 in either vector member_i or member_j.
Mark
Mark am 11 Mär. 2013
for additional clarity, rows 1 & 2, 5 & 6, 9 & 10 are regarding member_i, rows 3 & 4, 7 & 8, 11 & 12 are regarding member_j.
Matt J
Matt J am 11 Mär. 2013
Bearbeitet: Matt J am 11 Mär. 2013
You're going to need to be even clearer, I suspect...
member_i in your example consists of 3 numbers [1,1,2]. By what rule do I get from these 3 numbers to the row numbers 1 & 2, 5 & 6, 9 & 10? Moreover, once I have the row coordinates of the 1s, how do I then get the column coordinates?
Mark
Mark am 11 Mär. 2013
I've posted an image on twitter for more clarity. http://t.co/kgEGqjgudC
The 1's are always on the diagonal in their own little two by two matrix as shown in the image [1 0;0 1]. Each little two by two matrix represents the coordinates of member_i or member_j. So if the vector member_i(3,1) = 2 as is the case in this example the 9th and 10th rows would be used along with the 3rd and 4th columns. And if the vector member_j(3,1) = 3 as is the case in this example, the 11th & 12th rows would be used along with the 5th & 6th columns. So the columns are kind of grouped in a sense as well as the pairs of rows. I'm hoping the picture helps to clarify.
Matt J
Matt J am 11 Mär. 2013
Bearbeitet: Matt J am 11 Mär. 2013
The picture does clarify things, and see my answer below. According to your rules, however, the number of columns will not be 2*length(member_i) as you originally posted. The number of columns would have to be (at least)
2*max([member_i;member_j])

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Matt J
Matt J am 11 Mär. 2013

0 Stimmen

n=length(member_i);
I=1:2*n;
J=I;
J(1:2:end)=member_i(:).';
J(2:2:end)=member_j(:).';
Beta=kron(sparse(I,J,1), eye(2));

1 Kommentar

Mark
Mark am 11 Mär. 2013
Matt, thank you so much for your help. I will have to study this bit of code for awhile to understand exactly what's happening. I did a full(Beta) to check out what it looks like expanded. Works great.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Produkte

Gefragt:

am 11 Mär. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by