Amit
Amit

Reputation: 604

How to get this matrix

I have this matrix:

 S.No.  A         B  
 1     5268020   1756  
 2     15106230  5241  
 3     24298744  9591  
 4     23197375  9129  

I want to get a matrix which will have two columns [X,Y]. X will take values from S.No. and Y will can be either 1 or 0. For example, for 1 5268020 1756 there should be total 5268020 (1,0) i.e, (X,Y) pairs and 1756 (1,1) pairs.

How can I get this matrix in Octave ??

Upvotes: 0

Views: 101

Answers (1)

chl
chl

Reputation: 29447

If I understand your question correctly, you want to fill a matrix with repeated entries (x,0) and (x,1), where x=1...4, where repetition is determined by values found in column A and B. Given the values you supplied that's going to be a huge matrix (67,896,086 rows). So, you could try something like this (replace m below, which has less elements for illustrative purpose):

m = [1, 2, 1; 
     2, 3, 2; 
     3, 2, 1; 
     4, 2, 2];
res = [];
for k = 1:4
  res = [res ; [k*ones(m(k, 2), 1), zeros(m(k, 2), 1); 
                k*ones(m(k, 3), 1), ones(m(k, 3), 1)]];
endfor

which yields

res =

   1   0
   1   0
   1   1
   2   0
   2   0
   2   0
   2   1
   2   1
   3   0
   3   0
   3   1
   4   0
   4   0
   4   1
   4   1

Out of curiosity, is there any reason not to consider a matrix like

1  0  n
1  1  m
2  0  p
2  1  q
...

where n, m, p, q, are values found in columns A and B. This would probably be easier to handle , no?

Upvotes: 1

Related Questions