How to generate permutations of a matrix with given restrictions.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Ryan Shone
am 28 Sep. 2021
Kommentiert: Ryan Shone
am 29 Sep. 2021
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 Kommentare
Akzeptierte Antwort
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)) = [];
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!