Randi with string variables

조회 수: 15 (최근 30일)
Emilie04
Emilie04 2012년 11월 21일
Goodmorning everybody,
I'm trying to use the 'randi' function on Matlab (but this is for a script on Psychtoolbox). I would like to present a string value, instead of a numerical value. This is what I obtain :
First trial :
>> items = {'lapin', 'souris', 'voiture', 'saumon', 'vÈlo', 'table', 'cochon'};
>> randi(items{i});
*?? Subscript indices must either be real positive integers or logicals.>>*
Second trial :
>> items = {'lapin', 'souris', 'voiture', 'saumon', 'vÈlo', 'table', 'cochon'};
>> randi(items);
*??? Error using ==> randi
Inputs must be numeric.*
Third trial :
>> items = {'lapin', 'souris', 'voiture', 'saumon', 'vÈlo', 'table', 'cochon'};
>> randi(7)
ans =
4
How Matlab can show me the string values specified in 'items' with randi?
Thanks!!
-Emilie

답변 (3개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 21일
편집: Azzi Abdelmalek 2012년 11월 21일
items = {'lapin', 'souris', 'voiture', 'saumon', 'vÈlo', 'table', 'cochon'}
n=numel(items);
out=items(randi(n,n,1))
If you want to avoid repetition use
n=numel(items);
items(randperm(n))
  댓글 수: 2
Emilie04
Emilie04 2012년 11월 21일
Thank you :) That functions.
But I want to go father. I want that Matlab choses 1 value in the list, randomly and after its choice, it continues to read the list in the order defined in items = {'lapin', 'souris', 'voiture', 'saumon', 'vÈlo', 'table', 'cochon'};
For example : it choses, randomly 'saumon', and then it presents 'vélo' 'table', 'cochon'
Two problems : with this method, it always choses more than one items (and if I write randperm(1:7), that doesn't function).
>> items = {'lapin', 'souris', 'voiture', 'saumon', 'vÈlo', 'table', 'cochon'}; >> n=numel(items); >> SP=items(randperm(3))
StartPosition =
'souris' 'voiture' 'lapin'
>> PositionOrder=[StartPosition:7 1:(StartPosition-1)] ??? Undefined function or method 'colon' for input arguments of type 'cell'.
Thanks !
Jan
Jan 2012년 11월 21일
@Emilie: The error message means, that "StartPosition" is a cell. It seems like you have confused the StartPosition, which is most likely an index, with the index contents.
Do not use randperm(1:7) but randperm(7).

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


Jan
Jan 2012년 11월 21일
편집: Jan 2012년 11월 21일
I suggest to read the Getting Started chapters of Matlab. The documentation of randi (see "help randi", "doc randi") explain, that a number is replied and which inputs are needed. Then it is obvious that "randi(items)" must fail und you do not have to ask the forum for this.
items = {'lapin', 'souris', 'voiture', 'saumon', 'vÈlo', 'table', 'cochon'};
n = numel(items);
index = randi(n, 1);
selectedItems = items(index:n)
You see: "items" are your data, while "randi" and "index" are indicies only. To get the wanted indices, the actual data do not matter at all, therefore the creation of "index" does not require the contents of "item", but only its size.

Daniel Shub
Daniel Shub 2012년 11월 21일
Is this what you want?
items = {'lapin', 'souris', 'voiture', 'saumon', 'vÈlo', 'table', 'cochon'};
items(randi(length(items)):end)
  댓글 수: 1
Emilie04
Emilie04 2012년 11월 21일
Oh Yes thank you!!!! Two days on this function.
Tanks a lot :):)

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

카테고리

Help CenterFile Exchange에서 Image display and manipulation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by