How to generate permutations of a matrix with given restrictions.

1 Ansicht (letzte 30 Tage)
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.

Akzeptierte Antwort

Voss
Voss am 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 Kommentar
Ryan Shone
Ryan Shone am 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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by