File Exchange

## [B1, B2, B3,... Bn] = repeat2combine(dim, A1, A2, A3... An)

version 1.0.0.0 (4.33 KB) by Brayan Torres Z.

### Brayan Torres Z. (view profile)

Repeat arrays elements to get all combinations (General function, for any class and dimension).

Updated 14 Nov 2017

Repeat arrays elements to get all combinations.
Syntax
[B1, B2] = repeat2combine(dim, A1, A2)
[B1, B2, B3,... Bn] = repeat2combine(dim, A1, A2, A3... An)

Description
[B1, B2] = repeat2combine(dim, A1, A2) repeates the elements of the
arrays (e.g., column vectors) A1 and A2 along the dimension specified
by dim (e.g., 1) to get all combinations. The input arrays have not to
be of the same class.

For n-dimensional arrays, all elements along dimensions other than dim,
are considered as a single element, for instance, if A1 and A2 are 2D
arrays and dim is set to 1 (column), then each row are considered as a
single entity or element.

[B1, B2, B3,... Bn] = repeat2combine(dim, A1, A2, A3... An) repeats the
elements of all the input arrays A1, A2, A3... An, alongs the dimension
dim.
Input arguments
An - Arrays with elements to repeat.
Data type: numeric | char | cell | logical | categorical | table
struct | datetime | timeseries
Size : (any size)

Output arguments
Bn - Arrays with repeated elements.
Data type: (same as An)
Size : (same as An, but the dimension dim is expanded as much
as necessary)

Notes
The argument dim must be less or iqual than the number of dimensions of
each input array.

If the argument dim is set along a singleton dimension there are no
repetitions because there are an unique element, for instance:
if A1, A2 are row vectors, then [B1, B2] = repeat2combine(1, A1, A2)
will return B1 = A1 and B2 = A2.

Examples
Example 1: repeate elements of two numeric vectors along the first
and second dimensions to get all combinations.
A1 = [1, 2, 3];
A2 = [10, 20, 30];
[B1, B2] = repeat2combine(1, A1, A2); % Notice that along the first dim
% there are an unique element (row).
[B1, B2, B3] = repeat2combine(2, A1, A2, A3);

Example 2: repeate elements of three numeric vectors to get all
combinations in an unique array.
A1 = 1:2;
A2 = 10:10:30;
A3 = 100:100:200;
[B1, B2, B3] = repeat2combine(2, A1, A2, A3);
arrayOfCombinations = cat(1, B1, B2, B3);

Example 2: repeat elements of three character vectors to get all
combinations in an unique array.
A1 = 'abc';
A2 = 'xy';
A3 = 'jkl';
[B1, B2, B3] = repeat2combine(2, A1, A2, A3);
arrayOfCombinations = cat(1, B1, B2, B3);

Example 3: repeate elements of three cell vectors to get combinations
in an unique array.
A1 = {'a'; 'b'; 'c'};
A2 = {'x'; 'y'; 'z'};
A3 = {'1'; '2'; '3'};
[B1, B2, B3] = repeat2combine(1, A1, A2, A3);
arrayOfCombinations = cat(2, B1, B2, B3);

Example 4: repeat elements of three arrays with different class to get
all combinations.
A1 = [10, 20; 30, 40];
A2 = {'a', 'b', 'c'; 'd', 'e', 'f'};
A3 = struct('color', {'red'; 'blue'; 'grey'},...
'size', {'medium'; 'small'; 'large'});
[B1, B2, B3] = repeat2combine(1, A1, A2, A3); We cannot concatenate.

ndgrid permute fliplr

### Cite As

Brayan Torres Z. (2020). [B1, B2, B3,... Bn] = repeat2combine(dim, A1, A2, A3... An) (https://www.mathworks.com/matlabcentral/fileexchange/65046-b1-b2-b3-bn-repeat2combine-dim-a1-a2-a3-an), MATLAB Central File Exchange. Retrieved .
{"ft_lang_label":"__label__en","ft_lang_prob":0.6852274,"math_prob":0.9965355,"size":3125,"snap":"2020-34-2020-40","text_gpt3_token_len":1031,"char_repetition_ratio":0.17686638,"word_repetition_ratio":0.15619047,"special_character_ratio":0.35712,"punctuation_ratio":0.27222222,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9912886,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-09T18:25:47Z\",\"WARC-Record-ID\":\"<urn:uuid:69c12102-030c-426a-bf3b-923fe37b21c7>\",\"Content-Length\":\"83403\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:dbe9a0a5-719d-4f60-9ac1-f1046fd22be1>\",\"WARC-Concurrent-To\":\"<urn:uuid:cdd5dbf1-9026-44b4-bea9-172b63e243ff>\",\"WARC-IP-Address\":\"23.212.144.59\",\"WARC-Target-URI\":\"https://www.mathworks.com/matlabcentral/fileexchange/65046-b1-b2-b3-bn-repeat2combine-dim-a1-a2-a3-an\",\"WARC-Payload-Digest\":\"sha1:42Z7SYFTHXCB2FNRWX5AWQSOOOCN5G5A\",\"WARC-Block-Digest\":\"sha1:JVLT3SI3HGELJEQ7S5HFKR55RV5FPKH4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439738562.5_warc_CC-MAIN-20200809162458-20200809192458-00397.warc.gz\"}"}