Hello beautiful people,
Can anyone tell me that how I get random pair out of the following pairs?
I have these six pairs (1,5) (2,5) (3,3) (2,4) (2,5) (4,2)
these pairs represents x, y values/dimensiuons so I need one random pair like (2,5)....

답변 (1개)

the cyclist
the cyclist 2021년 2월 23일
편집: the cyclist 2021년 2월 23일

0 개 추천

How are these pairs currently stored?
This seems equivalent to just choosing a random number from 1-6, so you could do
idx = randi(6)
and choose the corresponding pair.

댓글 수: 12

marie lasz
marie lasz 2021년 2월 23일
편집: marie lasz 2021년 2월 23일
I don't know how I should store them but in a result I want a pair which should be random.
was trying this way but I don't want the pair (1,1) or so,... because I only need to choose between these pairs ....
key=10;
rng(key);
x=randi([1 5],1,1);
y=randi([1 5],1,1);
Ah, I see. You could do this:
M = [1,5;
2,5;
3,3;
2,4;
2,5;
4,2;];
xy_random = M(randi(6),:)
xy_random = 1×2
2 5
marie lasz
marie lasz 2021년 2월 23일
편집: marie lasz 2021년 2월 23일
Ok I tried and getting it but how can I get pair with a comma between two values like (2,5)?
the cyclist
the cyclist 2021년 2월 23일
Can you be specific about exactly how you want the output to be? Do you want a string? A numeric vector? Just something that displays to the screen in a certain way?
marie lasz
marie lasz 2021년 2월 23일
See i am defining an equation in which I have to insert x,y dimension and these pairs are actually dimensions which are not working without comma(,). So I need a pair which returns (2,5) or other random pair .
So, the frustration here for me is nothing you are saying is in terms of MATLAB syntax. So, again, do you need a string like
"(5,2)"
?
Or maybe a character array like
'(5,2)'
?
Can you please state in MATLAB syntax what your next step requires this pair of values to look like? It is very strange for MATLAB code to expect numeric values in something other than numeric format.
Walter Roberson
Walter Roberson 2021년 2월 23일
How are you inserting x,y dimension?
marie lasz
marie lasz 2021년 2월 23일
@the cyclist apologies as I am a beginner ,yes I need a string.
marie lasz
marie lasz 2021년 2월 23일
@Walter Roberson x y dimensions is related to different pairs which I wrote above in the question. I am inserting these dimensions like;where c is a constant value.
the cyclist
the cyclist 2021년 2월 23일
편집: the cyclist 2021년 2월 23일
M = [1,5;
2,5;
3,3;
2,4;
2,5;
4,2;];
xy_random = M(randi(6),:);
str_input = sprintf("(%d,%d)",xy_random)
str_input = "(2,5)"
a = b(idx(1), idx(2)) + c

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2021년 2월 23일

편집:

2021년 2월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by