Combinations of values of array of vectors (of different lengths) but ONLY in order the vectors appear in the array?

3 次查看(过去 30 天)
Hi, I'm trying to transcribe protein letters to DNA codons. I have a single row array that looks like:
[N1 N2 N3 N4 ...]
Each vector looks like:
N1 = {'UUU' 'UUG' 'UUC'} or
N2 = {'AUU' 'AUG' 'AUC' 'AAU' 'AAG'}
(number of cells can vary from 1 to 6)
I want every combination of the values of these vectors BUT only in the order they appear in the main [N1 N2] vector. For example, 'UUU AUU' and "UUU AUG' are desirable but 'AUU UUU' and 'AUG UUU' are NOT.
Also every combination must contain a value from each vector. So if the array has 20 'N' vectors then each combination needs 20 'three letter' strings.
I'm not much of a coder and I've ended up in a useless mess of nested loops. I think this is a problem for recursion but I'm stuck :(
Your help and time are greatly appreciated! :) Also please let me know if I can clarify anything.
edit: put examples in matlab syntax
  4 个评论
Evan Meichtry
Evan Meichtry 2016-10-21
Guillaume, I tried to fix the syntax. Hopefully it is clearer now. To answer your question everything is a single row.
David, that's a lot of combinations!! However, I only want combinations N cell arrays while keeping the actual N arrays in the same order, does that make sense? It's difficult to put into words. I hope that reduces the unique combinations enough.
Guillaume
Guillaume 2016-10-21
Evan, yes it is a lot clearer thanks. However, I assume that the main array is actually {N1 N2 N3 N4 ...} not [N1 N2 N3 N4 ...], otherwise, once you've concatenated the Ns, there's no way to know which N the char arrays came from.
David is completely right. Just picking one of 3 elements from 20 consecutive vectors in order is 3^20 = ~3.5e9 combinations. You will need at least 13 GB of memory just to store all combinations of indices Extend that to 6 elements per vector and that's ~3.5e15 combinations and 13 PB (PetaBytes) of memory.
You probably need to review your approach to the problem.

请先登录,再进行评论。

采纳的回答

Chaya N
Chaya N 2016-10-20
编辑:Chaya N 2016-10-20
If I understand your question right, I think that your problem is one of creating unique combinations out of unequal length vectors. Perhaps you should look up the combvec function.
As a sample, when I run the following code snippet:
x = [1,2];
y = [4,5,6];
z = [8,9];
combvec(x,y,z)
I get the following answer (read column-wise of course)
ans =
1 2 1 2 1 2 1 2 1 2 1 2
4 4 5 5 6 6 4 4 5 5 6 6
8 8 8 8 8 8 9 9 9 9 9 9
To me this sounds like exactly what you require.
Good Luck!
  5 个评论
Chaya N
Chaya N 2016-10-22
编辑:Chaya N 2016-10-22
Hello Evan, Are you trying to generate the same 27x3 cell array output_x from your multi-level indexing (I am concentrating on the above example for now, then we can extend it to larger data) or are you trying to put each combination (row) in a cell by itself (to produce a 27x1 cell array)? Could you please clarify?
Guillaume
Guillaume 2016-10-22
Evan, if you have whichever toolbox combvec comes with then use that as it abstracts all the difficulties of generating outputs and inputs for ndgrid. Otherwise, here is the generic way of using ndgrid:
%input: RNA_code, a cell array of arbitrary size containing vectors also of arbitrary size.
ndgridargs = cellfun(@(v) 1:numel(v), RNA_code, 'UniformOutput', false); %generate inputs for ndgrid
[ndgridargs{:}] = ndgrid(ndgridargs{:}); %reuse cell array for output, since it's the right size
allcombs = cellfun(@(c, idx) reshape(c(idx), [], 1), RNA_code, ndgridargs, 'UniformOutput', false); %use ndgridargs to index respective vector after reshaping into columns
allcombs = [allcombs{:}] %concatenate all horizontally

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by