How can i operate on every newly created vector in a for loop?

1 次查看(过去 30 天)
I am trying to build all possible permutations of a vector following this rule: Subsequene entries in the vector can either decrease by exactly 1 or increase by any amount. I start with the vector [1] and then modify it and append 2 to create [1 2] and [2 1]. Notice that each new vector should also probude 2 subsequent vectors, one with the new value of n appended on the very end, and one with the new value of n appended right before (n-1). I cannot seem to get MatLab to operate on every single newly created vector while in the foor loop (since more and more vectors are created every iteration).
clear
n=input('Input value for n\n'); %Set n-value.
P=[]; %Define an empty permutation matrix.
P(1,:)=[1]; % Valid Permutation for n=1
M{1} = P; % Putting that permutation in a call array.
for i=2:n
v1=[M{i-1} i]; %This creates all the [1 2 3 ... n] perms
v2=[M{i-1}(1:find(M{i-1}==(i-1)-1)) i M{i-1}(find(M{i-1}==(i-1)):end)]; % This creates all [1 2 ... (n-1) n] permutations.
% It doesnt look like my script operates on EVERY newly created vector to create ALL valid permutations.
M{i}=v1;
N{i}=v2;
end
G=[M; N]
for i=1:n
G{i}
end
How can I get my script to apply the two operations in the for loop to EVERY newly created vector during the previous iteration?
  2 个评论
badscience
badscience 2019-2-1
Sorry, it is a little hard to explain if you are not the one that knows exactly what the problem is. Let me try to explain with an example of then I run the script for, say, n=4.
What I want to happen is this: The vector [1] is already created before the loop. The script needs to append a 2 to this vector and create two new vectors, [1 2] and [2 1]. Then these vectors need to be placed in the cell array. Now it needs to perform the same appending operation on BOTH the newly created vectors. So in the case of [1 2], it needs to append a 3 on the end to create [1 2 3] and then append a 3 before the 2 to create [1 3 2]. Then it has to go back and do the same thing to the vector [2 1]. Append a 3 on the end to create [2 1 3], and append a 3 before the 2 to create [3 2 1]. So after this iteration, [1 2] -> [1 2 3], [1 3 2] and [2 1] -> [2 1 3], [3 2 1].
So in the very initial iteration it has to create 2 new vectors. During the second iteration, it has to create 4 new vectors. During the third iteration it will have to make 8 new vectors and so on. It seems I can only get it to create 2 new vectors each time, which leads me to believe it is only operating on a single vector each iteration, and not all newly created vectors.
I have been looking into cellfun, but as far as writing an actual function to operate on these cells, I am having a hard time becase I am pretty new to cell arrays and cannot get them to work correctly in the function.
Sorry if that is still a little confusing, its the best I can do to explain it!

请先登录,再进行评论。

回答(1 个)

Bob Thompson
Bob Thompson 2019-2-1
Ok, I think maybe all you need to do is add a second internal loop to loop through all the cells of the previous iteration.
for i = 2:n;
for j = 1:length(M{i-1})
v1 = [M{i-1}{j} i];
v2 = [M{i-1}{j}(...% Other stuff)];
M{i}{j} = v1;
N{i}{j} = v2;
end
end
I tried to keep this as similar to what you already had, but basically you should have a cell array M which contains a cell for each iteration. Then each of those cells will contain an array of the different arrays.
  2 个评论
badscience
badscience 2019-2-1
So I ran this:
for i = 2:n;
for j = 1:length(M{i-1})
v1 = [M{i-1}{j} i];
v2 = [M{i-1}{j}(1:find(M{i-1}==(i-1)-1)) i M{i-1}(find(M{i-1}==(i-1)):end)];
M{i}{j} = v1;
N{i}{j} = v2;
end
end
And I got the error
"Brace indexing is not supported for variables of this type."
"Error in columnPNtableauxRecursionAlg2 (line 35)"
v1 = [M{i-1}{j} i];
So I removed the {j}'s to run this:
for i=2:n
for j = 1:length(M{i-1})
v1 = [M{i-1} i];
v2 = [M{i-1}(1:find(M{i-1}==(i-1)-1)) i M{i-1}(find(M{i-1}==(i-1)):end)];
M{i}{j} = v1;
N{i}{j} = v2;
end
end
And now I get the error "Undefined operator '==' for input arguments of type 'cell'."
Which is weird because find(M{i-1}==(i-1)-1) should just return an integer, should it not?
Bob Thompson
Bob Thompson 2019-2-1
No, not if your variable got defined with the internal cells. I would suggest returning to the first error and determining the class of i and M{i-1}{j}. It's possible that they are not the same class, and the code is trying to concat a cell and a double or something.
You also need to make sure you are initializing your M{1} to a second level of cells (M{1}{1}) in order for M{i-1}{j} to work the first time through.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by