Assigning new matrix values from another matrix's rows

Hello All.
I'm wondering if you can help with a simple task.
I have a n*3 matrix with 'triplets' of bits that can be reoccuring within this matrix, e.g.
triplets=[0 1 1; 1 0 1;...];
I'd like to create a new matrix that is n*2 in size based on what the triplet is, e.g. whenever there is a (0 1 1) it should give a pair of values in the new matrix, e.g. (2 2) and (1 0 1) should give (2 0) and so on. So the new matrix should look like
pairs= [2 2; 2 0;...].
Basically I'd like to create a look-up table based on the triplets.
Thank you for your help!

2 Comments

Is there a pattern I'm missing or is the conversion from triplet to pair arbitrarty?
Adam,
The pairing is chosen by me, there is no conversion.

Sign in to comment.

 Accepted Answer

I would just convert the bits into a single integer index,
index=triplets*[4;2;1];
and then perform lookup into an appropriately prepared Nx2 matrix, T.
T(index,:)

3 Comments

Thank you, Matt.
My Matlab knowledge is limited, but I will need the new matrix with the pairs, as it will be used later on. I'm not entirely sure your solution creates that new matrix, as I can't check right now. Sorry if I'm not understanding correctly!
Yes, it does.
new_matrix=T(triplets*[4;2;1] +1 , :)
You must simply provide the appropriate T. As for how to construct T, you haven't said what your given data looks like, so it's hard to be specific, but here is how you would do it based on Adam's example conversion table
conversionTable = {...
[0 0 0], [0 1];
[0 0 1], [0 2];
[0 1 0], [0 3];
[1 0 0], [0 4];
[0 1 1], [2 2];
[1 0 1], [2 0];
[1 1 1], [3 0];
[1 1 0], [3 0];
};
indices=vertcat(conversionTable{:,1})*[4;2;1]+1;
outputs=vertcat(conversionTable{:,2});
T(indices,:)=outputs;
And now to perform an example look-up
triplets=[0 0 1; 1 1 0];
new_matrix=T(triplets*[4;2;1] +1 , :)
new_matrix =
0 2
3 0
Matt,
Thanks for you answer, I just didn't think enough beyond your last line of code.
I am very grateful!
Tamás

Sign in to comment.

More Answers (1)

If the converstion from [nx3] to [nx2] is arbitrary (ie, not calculable), here's how to create a lookup table and do the conversion.
Note that the conversion table could use single integer values proposed by Matt J if all of the triplets are binary.
triplets= [...
0 1 1;
1 0 1;
0 0 0;
1 1 1;
1 1 0;
0 1 0];
% pattern on left will be encoded as pattern on right
% For this demo, the left column does not have all possible permutations.
conversionTable = {...
[0 0 0], [0 1];
[0 0 1], [0 2];
[0 1 0], [0 3];
[1 0 0], [0 4];
[0 1 1], [2 2];
[1 0 1], [2 0];
[1 1 1], [3 0];
[1 1 0], [3 0];
};
% separate columns from table into matrices
ctIn = cell2mat(conversionTable(:,1));
ctOut = cell2mat(conversionTable(:,2));
% match rows of triplets with rows of ctIn
[~, ctInIdx] = ismember(triplets, ctIn, 'rows'); %row numbers of ctIn
% convert to [n x 2]
conversion = ctOut(ctInIdx, :);

2 Comments

Adam,
I do not have access to Matlab right now, but what I understand is that this will pair the "pairs" with the appropriate triplets no matter how large the triplet matrix is. Am I correct?
I believe it does, and would accept both your answer and Matt's, but I can only select one answer, unfortunately.
I really appreciate your solution too and that you took the time. Thank you!

Sign in to comment.

Products

Release

R2015b

Asked:

on 14 Feb 2019

Edited:

on 14 Feb 2019

Community Treasure Hunt

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

Start Hunting!