# Error using randi Size inputs must be scalar (Matlab)

1 Ansicht (letzte 30 Tage)
high speed am 12 Apr. 2021
Kommentiert: Jan am 13 Apr. 2021
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 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Rik am 12 Apr. 2021
How are you making sure B and A(i) are positive scalars?

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Jan am 12 Apr. 2021
Bearbeitet: Jan am 12 Apr. 2021
"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 Kommentare3 ältere Kommentare anzeigen3 ältere Kommentare ausblenden
high speed am 13 Apr. 2021
@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 am 13 Apr. 2021
@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).

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by