Hi everyone. I want to create a new matrix using a rand function and the values of other matrix.

조회 수: 1 (최근 30일)
I have a matrix (n,2) that have in the first column the min values and in the second column the max values. I want to create a new matrix (n,100) with uniform distributions in each row using the rand function and picking as MIN value the first value and as MAX value the second value from each row related to the matrix (n,2). In my code it always get the error "Size inputs must be scalar" in the the rand function.
Code:
p=length(t);
ii=1;
pdf=[];
while ii<=p
IMIN=t(ii,1);
IMAX=t(ii,2);
pdf=rand([IMIN IMAX],p,100);
ii=ii+1
end

채택된 답변

dpb
dpb 2016년 5월 4일
IMIN=t(:,1);
IMAX=t(:,2);
pdf=bsxfun(@plus,IMIN,bsxfun(@times,(IMAX-IMIN),rand(p,100)))
You can dispense with the intermediary IMIN/IMAX arrays in place of t, of course, used simply for legibility to aid in seeing the solution logic.

추가 답변 (1개)

Kevin
Kevin 2016년 5월 4일
Are you thinking about generating uniformly distributed random integers? If yes, then you need to replace rand with randi in your code.
  댓글 수: 1
dpb
dpb 2016년 5월 4일
Good point -- if that's the case, the following may be more suitable--
pdf=cell2mat(arrayfun(@(imn,imx,p) randi([imn imx],1,p), ...
IMIN,IMAX,'uniformoutput',false));
will, however.

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

카테고리

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