Selection with no direct repeatition
Mostra commenti meno recenti
Hello everyone,
I want to create 10 vectors with the following conditions:
Element number (i) is not equal to element number (i+1) and they have to be selected from set "a".
Length of each vector is 10 elements.
I am new to programming and I'd appreciate it if you can help me.
Here is me attempt:
%------------------------------------------------------------------------%
clear variables;
close all;
clc;
%------------------------------------------------------------------------%
%% variables definition
n=10;
a=[0 4/3 2];
n1=nan;
%------------------------------------------------------------------------%
%% memory allocation
%------------------------------------------------------------------------%
i1=zeros(1,n);
i2=zeros(1,n);
n1=zeros(1,n);
n2=zeros(1,n);
%------------------------------------------------------------------------%
%% selection process
for i=1:n
i1(i) = randi(length(a));
n1(i) = a(i1(i));
i2(i) = randi((length(a)-1)); % to provide better selection ( picking from 2 element instead of 3)
i2(i) = i2(i) + (i2(i) >= i1(i));
n2(i) = a(i2(i));
while (n1==n2)
n2(i) = a(i2(i));
end
n1=n2;
end
n2
Risposta accettata
Più risposte (1)
Ameer Hamza
il 22 Apr 2020
>> randperm(10)
ans =
2 3 9 7 5 8 10 6 4 1
1 Commento
Hamzah Faraj
il 22 Apr 2020
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!