How to generate permutations of a matrix with given restrictions.

1 view (last 30 days)
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.

Accepted Answer

Voss
Voss on 28 Sep 2021
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 Comment
Ryan Shone
Ryan Shone on 29 Sep 2021
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!

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!