Error using randi Size inputs must be scalar (Matlab)

조회 수: 2 (최근 30일)
high speed
high speed 2021년 4월 12일
댓글: Jan 2021년 4월 13일
I have entries parameters A and B, and I have to obtain C with for loop, where C is the random subset of B of size Ai
I program this
for i=1:n
C=randi([0,1],B,A(i));
end
But I get the error (Error using randi ==> inputs must be scalar)
How can I fixe the problem
  댓글 수: 1
Rik
Rik 2021년 4월 12일
How are you making sure B and A(i) are positive scalars?

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

채택된 답변

Jan
Jan 2021년 4월 12일
편집: Jan 2021년 4월 12일
"C is the random subset of B of size Ai" :
for i = 1:n
% Without repetitions:
C = B(randperm(numel(B), A(i)));
% Or with repetitions:
C = B(randi([1, numel(B)], 1, A(i)));
... use C now
end
  댓글 수: 5
high speed
high speed 2021년 4월 13일
@Jan Unfortunately, that's not what I want. Because I must program in a way that C is the random subset of B of size Ai. And in your example you didn't consider the size Ai
Jan
Jan 2021년 4월 13일
@high speed: Sorry, what? Yes, in my example, I've used 4 to clarify what "repetition" means. You have asked for this detail. But the code in my answer does contain A(i).

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

추가 답변 (0개)

카테고리

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