필터 지우기
필터 지우기

how to avoid repeat numbers in a row

조회 수: 1 (최근 30일)
imola
imola 2015년 3월 19일
댓글: imola 2015년 3월 19일
Dear All,
I have this row
A=[1 2 3 4 5 6];
I tried to find random permutation on the vector but in many cases I got a row with repetition numbers for example
z=[1 5 2 4 1 5];or z=[1 5 2 4 1 1];
I want to avoid the repetition in this code but it is totally wrong, at first I should assign the missing numbers in such way, here is [3 6].
zc=[3 6 ];
e=size(zc,2);
z=[1 5 2 4 1 5];
for i=1:6
z(i)==z
s = find((z(i)==z) == 1)
ss=size(s,2);
for j=2:ss
for k=1:e
z(j)=zc(k)
end
end
end
I don't want to use a function from matlab to get the permutation, I need to fix my row or any permutation exchange file like (perm file ). If anyone help me for it and save me I will be grateful.
regards,
Imola
  댓글 수: 2
Guillaume
Guillaume 2015년 3월 19일
I don't want to use a function from matlab to get the permutation:
Does that mean that you've written your own uniform number generator? Otherwise, at some point you'll have to use rand, so you just might as well use randperm.
imola
imola 2015년 3월 19일
yes exactly I used randperm.

댓글을 달려면 로그인하십시오.

채택된 답변

Sean de Wolski
Sean de Wolski 2015년 3월 19일
[~,x] = sort(rand(1,6))
No randperm
  댓글 수: 1
imola
imola 2015년 3월 19일
I m sorry Sean,
but I wanted to accept Guillaume answer and by mistake I chose yours, how I change it because his answer was great.
regards

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guillaume
Guillaume 2015년 3월 19일
It seems to me that your effort would be better spent on fixing the code that generate these invalid permutation rather than fixing the permutation afterward.
Anyway, one way to do what you want:
z = [1 5 2 4 1 1];
zc = [3 6];
%build a cell array containing the indices of repeated values:
repindices = accumarray(z', 1:numel(z), [], @(v) {v(2:end)'})';
%convert cell array into vector:
repindices = [repindices{:}];
%replace values at indices by replacement:
z(repindices) = zc
  댓글 수: 4
Guillaume
Guillaume 2015년 3월 19일
If you're asking how to build zc:
zc = setdiff(1:6, z)
imola
imola 2015년 3월 19일
Dear Guillaume,
I'm so sorry, by mistake I chose Sean's answer. Forgave me.
Regards

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by