Index exceeds matrix dimensions. Error in RandomSelection (line 9) xx=r(1:numOfBits);

1 回表示 (過去 30 日間)
Norhan Ayoub
Norhan Ayoub 2021 年 3 月 30 日
回答済み: Esen Ozbay 2021 年 3 月 30 日
function [xx]=RandomSelection(numOfBits,max)
% This function generates unrepeated random numbers ranges from 1 to max
% x1=0;
xx= zeros(1, numOfBits);
r = randperm(max);
xx=r(1:numOfBits);
%
end
%

回答 (1 件)

Esen Ozbay
Esen Ozbay 2021 年 3 月 30 日
You will get this error if numOfBits is greater than max.
For example, if max = 5 and numOfBits = 7, then r will be a 1x5 vector and you will try to access the seventh element of a 5-element array.
By the way, the line
xx= zeros(1, numOfBits);
is not necessary since you are redefining xx below.

製品


リリース

R2013a

Community Treasure Hunt

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

Start Hunting!

Translated by