Generate combinations of Cells that contain text

12 Ansichten (letzte 30 Tage)
Andreas Georgakakos
Andreas Georgakakos am 4 Apr. 2018
Kommentiert: Guillaume am 24 Mai 2019
Hi there,
I am trying to generate all the combinations of certain cells so that each combination will include only one element of each vector/category. For example:
A = {'Square'; 'Rectangle'}
B = {'Heavy'; 'Light'; 'Medium'}
C = {'West'; 'East', 'South'; 'North'}
For output, I want a matrix that will have the following structure:
D = { 'Square-Heavy-West'; 'Square-Heavy-East' etc.}
If this would be possible without using cells, I would appreciate your thoughts.
Thank you in advance,
Andreas
  5 Kommentare
Andreas Georgakakos
Andreas Georgakakos am 4 Apr. 2018
I am open to using cells as well, just wondering if there is an alternative. But as I said, cells are fine too.
Stephen23
Stephen23 am 21 Mai 2019
"...but I don't believe it is possible to make an array of strings without doing cells, structures, or a table"
String arrays were introduced in R2016b. These are by definition an array of strings.

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Stephen23
Stephen23 am 21 Mai 2019
Bearbeitet: Stephen23 am 21 Mai 2019
Simple and efficient with ndgrid and strcat:
>> [Cx,Bx,Ax] = ndgrid(1:numel(C),1:numel(B),1:numel(A));
>> D = strcat(A(Ax(:)),'-',B(Bx(:)),'-',C(Cx(:)))
D =
'Square-Heavy-West'
'Square-Heavy-East'
'Square-Heavy-South'
'Square-Heavy-North'
'Square-Light-West'
'Square-Light-East'
'Square-Light-South'
'Square-Light-North'
'Square-Medium-West'
'Square-Medium-East'
'Square-Medium-South'
'Square-Medium-North'
'Rectangle-Heavy-West'
'Rectangle-Heavy-East'
'Rectangle-Heavy-South'
'Rectangle-Heavy-North'
'Rectangle-Light-West'
'Rectangle-Light-East'
'Rectangle-Light-South'
'Rectangle-Light-North'
'Rectangle-Medium-West'
'Rectangle-Medium-East'
'Rectangle-Medium-South'
'Rectangle-Medium-North'
"If this would be possible without using cells, I would appreciate your thoughts."
You could use string arrays, but the methods to generate all combinations would be exactly the same.
  4 Kommentare
Rik
Rik am 24 Mai 2019
RE: strfind, that is one of the examples where Octave sticks to the documented behavior, as it throws an error on numeric input. I know this question is not related to Octave, but I just wanted to put it out there.
Guillaume
Guillaume am 24 Mai 2019
I've always felt a bit ambivalent about the fact that strfind can find sequences in numeric arrays. It is very handy (and you'll find it suggested every now and then on Answers) but a function with such a name shouldn't deal with numbers.
The best thing would be to either change the name (FindSequence) or simply duplicate the function with a new name that clearly applies to numerics.

Melden Sie sich an, um zu kommentieren.


Fabian Torres
Fabian Torres am 21 Mai 2019
This worked for me but I am not sure about how fast can it be compared with the loop, but still more elegant:
A = {'Square'; 'Rectangle'}; nA = size(A, 1);
B = {'Heavy'; 'Light'; 'Medium'}; nB = size(B, 1);
C = {'West'; 'East'; 'South'; 'North'}; nC = size(C, 1);
D = reshape(strcat(repmat(B, 1, nC), '-', repmat(C', nB, 1))', [], 1);
nD = size(D, 1);
D = reshape(strcat(repmat(A, 1, nD), '-', repmat(D', nA, 1))', [], 1);

Bob Thompson
Bob Thompson am 4 Apr. 2018
If somebody knows a way to conduct this without loops feel free to mention it, but I do know that you can record all possible combinations using a set of for loops. Indexing can be a pain though if you just want a series of rows. Easier to look at as an output, just tricky to code.
lenA = size(A,1);
lenB = size(B,1);
lenC = size(C,1);
for first = 1:lenA; % Loop through first matrix
for second = 1:lenB; % Loop through second matrix
for third = 1:lenC; % Loop through third matrix
D{(first-1)*lenB*lenC+(second-1)*lenC+third,1} = A{first}; % Record word from first
D{(first-1)*lenB*lenC+(second-1)*lenC+third,2} = B{second}; % Record word from second
D{(first-1)*lenB*lenC+(second-1)*lenC+third,3} = C{third}; % Record word from third
% End single combination
end
end
end

Community Treasure Hunt

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

Start Hunting!

Translated by