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.

采纳的回答

Voss
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)) = [];
  1 个评论
Ryan Shone
Ryan Shone 2021-9-29
Brilliant and speedy, stellar response. Works a charm, working through the logic in my head by using this code helped as well so I appreciate you taking the time!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by