permutation matrix for process simulation

3 ビュー (過去 30 日間)
bruno ebel
bruno ebel 2020 年 2 月 27 日
コメント済み: bruno ebel 2020 年 2 月 28 日
Hello,
I'd like to test the combined effect of different filtration unit (diafiltration, ultrafiltration,...) and used them in different order. For now I have a manual code that works with a small number of unit filtration, but I would like to find something more generic and that would work with a N filtration units. For that, I need a matrix that contains the different possibilities.
The idea is to test firstly all the filtration units separately and then combine them gradualy while permuting the order.
Separator 1
Separator 2
...
Separator 1 / Separator 2
Separator 2 / Separator 1
Separator 1 / Separator 3
...
Separator 1 / Separator 2 / Separator 3
...
a=1; %Separator1
b=2; %Separator2
c=3; %Separator3
d=4; %Separator4
a1=[perms(a),0,0,0];
a2=[perms(b),0,0,0];
a3=[perms(c),0,0,0];
a4=[perms(d),0,0,0];
a5=[perms([a,b]),zeros(2,2)];
a6=[perms([a,c]),zeros(2,2)];
a7=[perms([a,d]),zeros(2,2)];
a8=[perms([b,c]),zeros(2,2)];
a9=[perms([b,d]),zeros(2,2)];
a10=[perms([c,d]),zeros(2,2)];
a11=[perms([a,b,c]),zeros(6,1)];
a12=[perms([a,b,d]),zeros(6,1)];
a13=[perms([b,c,d]),zeros(6,1)];
a14=perms([a,b,c,d]);
A=[a1;a2;a3;a4;a5;a6;a7;a8;a9;a10;a11;a12;a13;a14];
the results I need is that:
A=
1 0 0 0
2 0 0 0
3 0 0 0
4 0 0 0
2 1 0 0
1 2 0 0
3 1 0 0
1 3 0 0
4 1 0 0
1 4 0 0
3 2 0 0
...
thanks by advance,
bruno

採用された回答

Jon
Jon 2020 年 2 月 27 日
編集済み: Jon 2020 年 2 月 27 日
You should be able to loop using perms and nchoosek to do what you need.
Something like this:
% generate filtration test matrix
numFilters = 4
filters = 1:numFilters
A = [];
for i = 1:numFilters
% generate a matrix with all the ways of choosing the current number of filters
C = nchoosek(filters,i);
% loop through each of the possible ways of choosing this many filters
for j = 1:size(C,1);
% get all of the permutations of the current selection
B = perms(C(j,:));
Z = zeros(size(B,1),numFilters-size(B,2));
% add the new block of permutations and an appropriately sized block of zeros
A = [A;[B Z]];
end
end
By the way, I think in your manual code you forgot the permutations of [1 3 4] ie perms([a,c,d])
The above code grows the A matrix in a loop, rather than having it preallocated as is recommended for better memory management/performance. If the performance mattered you could probably work out how many rows A will have in advance and preallocate it, but the combinatorics were a little daunting :)
  4 件のコメント
Jon
Jon 2020 年 2 月 28 日
Hi Bruno, Glad to hear that this code did exactly what you wanted. Hopefully you can combine it with Stephen's suggestion to use a table to have the unit names directly in it. If this answers your question, when you have a chance it would be good to "accept" the answer. This way others who may have a similar problem will know that an answer is available
bruno ebel
bruno ebel 2020 年 2 月 28 日
Ok thanks a lot, i will test the table.
thanks again,
bruno

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by