Error using randi Size inputs must be scalar (Matlab)
5 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
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 个评论
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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!