How to generate permutations of a matrix with given restrictions.
1 次查看(过去 30 天)
显示 更早的评论
Hi,
So first of all I have tried using permpos located here: PERMPOS - File Exchange - MATLAB Central (mathworks.com)
This successful helped me generate all permutations for 5 randomly distrubted 1ns on a 7 x 7 matrix.
My question now however is, is there a way to do the same quickly while feeding in 'rules'?
To explain, I have used the below code to generate as described above.
clear;clc;
%Generate Permutations
ns = 5; %Number of sources switched on
n = 49; %Generates 7x7 array
[A] = permpos(ns,n); %Generates all permutations
B = reshape(A',[sqrt(n),sqrt(n),length(A)]);
%%Plotting
Now however I would like to do the same but populate this matrix with 10% of it being 1 like above, however to generate all permutations where 10% is 1 but remove the permutations where there are two ones directly next to one another in x or y.
So for example:
x = [1,0,0;
0,0,1];
x2 = [1,0,0;
1,0,0];
x would be fine whereas x2 wouldn't.
Thanks! I hope this makes sense.
0 个评论
采纳的回答
Voss
2021-9-28
Here's one way. First generate all permutations like you're doing, then do the following to remove those permutations from B that have adjacent 1's:
B(:,:,any(any(diff(B,1,2) == 0 & B(:,1:end-1,:) == 1,1),2) | any(any(diff(B,1,1) == 0 & B(1:end-1,:,:) == 1,2),1)) = [];
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!