Can I create cells inside cells in for loop?

12 views (last 30 days)
Greetings,
during coding of my test model, I've stumbled across one issue, that holds me back. I would love to know whether i can resolve this issue, or if there is any other easier solution.
To put it short, I need to create cells inside of cells in every iteration of my for-loop. In other words, with every iteration of "i" i need to go one stage deeper:
i=1 -> C=cell(1,2)
i=2 -> C{1,2}=cell(1,2)
i=3 -> C{1,2}{1,2}=cell(1,2) .... i=8
I am preparing this model for moment, when I will be given different number of iterations, thus I dont have to make structure of the cells before putting in the numbers.
c=cell(1,2);
for j=1:8
for i=1:3
%c{i,j}=cell(1,1)
%here i would like to generate desired cell structure
end
end
% c{1,2}=cell(1,1)
% c{1,2}{1,2}=cell(1,1)
.
.
.
I am making this for sorting algorithm of 9 numbers with 3 conditins.
Many thanks, and apologize me for my english, I am not native speaker :)
---------------------------------------------------------
If you want to get bigger picture of the problem:
I am making model of Job-shop scheduling problem, that needs to resolve order of 3 products, that are made on 3 different machines. Every product needs to go to every machine, but in different order. To sum it up I am left with 9 production actions. I am making model, that will attempt to find fastest schedule.
action ID's
  • Product1: 1-2-3
  • Product2: 4-5-6
  • Product3: 7-8-9
First I am trying to approach this with "full" model. I am making series of numbers, that I could then search through. These series have 3 similar conditions:
  • 1 has to go before 2 and 2 has to go before 3
  • 4 has to go before 5 and 5 has to go before 6
  • 7 has to go before 8 and 8 has to go before 9
So possible outcomes are for example
  • 1-2-3-4-5-6-7-8-9
  • 1-2-3-7-8-9-4-5-6
  • 1-4-7-2-5-8-3-6-9 etc (there should be around 296 solutions)
I want to create cell structure that will have in first layer made of 3 initial possible action (1,4,7), than in the next cell to the cell that contains initial number, for example 1, will have cell structure with number 1 in first column and next possible actions (2,4,7) in the second collumn etc. In the last layer I will have all possible solutions.
I still dont know whether this is good approach and if I could then make good searching mechanism for easy selection of final rows and finding best solutions after testing it.
This is early stage of my diploma thesis. I would like to apply some smart algorithms in the future, that will create state space differently.
If you have different suggestion, how to get this roughly 296 arrays of 9 numbers, i am one ear :)
Thanks, Michal
  5 Comments
Stephen23
Stephen23 on 6 Sep 2023
You might find recursive programming useful, which would avoid storing all of the data.

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 6 Sep 2023
Edited: Bruno Luong on 6 Sep 2023
"there should be around 296 solutions"
Really? We find 1680
comb = combr({1 2 3}, {4 5 6}, {7 8 9})
comb = 1680×9
1 2 3 4 5 6 7 8 9 1 2 3 4 5 7 6 8 9 1 2 3 4 5 7 8 6 9 1 2 3 4 5 7 8 9 6 1 2 3 4 7 5 6 8 9 1 2 3 4 7 5 8 6 9 1 2 3 4 7 5 8 9 6 1 2 3 4 7 8 5 6 9 1 2 3 4 7 8 5 9 6 1 2 3 4 7 8 9 5 6
% recursive engine
function comb = combr(varargin)
c = varargin;
c(cellfun(@isempty, c)) = [];
comb = zeros(1,0);
for k=1:numel(c)
d = c;
x = d{k}{1};
d{k}(1) = [];
sk = combr(d{:});
comb= [comb; [repmat(x, size(sk,1), 1), sk]];
end
end
  2 Comments
Bruno Luong
Bruno Luong on 6 Sep 2023
This will create a nestes structures and not cell, as the field names will make it clearer. But you'll see, it is nightmare to see what it contains rather than the combinations put as rows of the 2D array of solutions
s = combs({1 2 3}, {4 5 6}, {7 8 9})
function s = combs(varargin)
c = varargin;
c(cellfun(@isempty, c)) = [];
n = numel(c);
s = struct('val', cell(1,n), 'next', cell(1,n));
for k=1:n
d = c;
val = d{k}{1};
d{k}(1) = [];
sk = combs(d{:});
s(k) = struct('val', val, 'next', sk);
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!