Assigning new matrix values from another matrix's rows
Show older comments
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
Adam Danz
on 14 Feb 2019
Is there a pattern I'm missing or is the conversion from triplet to pair arbitrarty?
Tamás Lengyel
on 14 Feb 2019
Accepted Answer
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
Tamás Lengyel
on 14 Feb 2019
Edited: Tamás Lengyel
on 14 Feb 2019
Adam Danz
on 14 Feb 2019
Glad it worked out!
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!