Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
help n=12 for I=1:n p(I) =rand p(I) =28×p(I) p(I)=ceil(p(I)) end I need to put constraint on this code must be theres no redanandnt values ..so if n=28 p will be all the numbers from 1:28 but in random
    조회 수: 2 (최근 30일)
  
       이전 댓글 표시
    
 n=12 
for I=1:n  
p(I) =rand
p(I) =28×p(I) 
p(I)=ceil(p(I))
end 
I need to put constraint on this code must be theres no redanandnt values ..so if n=28 p will be all the numbers from 1:28  but in random
댓글 수: 1
  Adam
      
      
 2015년 2월 10일
				Please keep code in the body of the question not in the title. The title should be a short description of the problem.
Also please use the {} Code block to include formatted code.
답변 (2개)
  Star Strider
      
      
 2015년 2월 10일
        ‘I need to put constraint on this code must be theres no redanandnt values ..so if n=28 p will be all the numbers from 1:28  but in random’
The easiest way is to use the randperm function:
n = 12;
p = randperm(n);
댓글 수: 5
  Star Strider
      
      
 2015년 2월 10일
				@Adam — Thank you for your Comment!
Our friendly randperm appears frequently in Answers. That’s how I learned about it, since I don’t otherwise need to use it that often.
  Star Strider
      
      
 2015년 2월 10일
				
  Adam
      
      
 2015년 2월 10일
        Something like this would work:
nums = 1:28;
n = 28;
p = zeros(1,n);
count = numel( nums );
for i = 1:n
  idx = randi(count);
  p(i) = nums( idx );
  nums( idx ) = [];
  count = count - 1;
end
Not necessarily the most efficient way of doing it, but it will guarantee uniqueness so long as n is at most 28 (or whatever value you set as the maximum of nums.
댓글 수: 2
  Adam
      
      
 2015년 2월 10일
				replace:
randi( count )
with
round( rand( count ) * n )
and I think that should give you the correct answer.
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


