Hi, I want to call data in the matrix by using string which is defined by rand command.
A=[1 2]
B=[2 3]
C=[3 5]
corners = 'ABC'
randompick = corners(randi(numel(corners)))
disp(randompick)
randompick(1)
Here I cant get any first row numbers of the A, B, and C.
What's wrong in this script?
Thanks.
Desired answer is
Same as what A(1), B(1) or C(1) give after randomly chosing.

2 件のコメント

madhan ravi
madhan ravi 2019 年 2 月 5 日
what‘s the desired output?
FURKAN CEVAHIR
FURKAN CEVAHIR 2019 年 2 月 5 日
Same as what A(1), B(1) or C(1) give after randomly chosing.

サインインしてコメントする。

 採用された回答

KSSV
KSSV 2019 年 2 月 5 日

0 投票

A=[1 2
2 3
3 5] ;
corners = 1:size(A,1) ;
randompick = corners(randi(numel(corners)))
disp(randompick)
idx = randompick(1) ;
iwant = A(idx,1)

6 件のコメント

madhan ravi
madhan ravi 2019 年 2 月 5 日
? OP has mentioned "I want to call data in the matrix by using string which is defined by rand command."
corners = 'ABC'
KSSV
KSSV 2019 年 2 月 5 日
I know....what is the necessity to call it by strings? At the end if strings are there..he has to use eval which is not advised. When there is a option of calling by indices why to use strings?
FURKAN CEVAHIR
FURKAN CEVAHIR 2019 年 2 月 5 日
編集済み: FURKAN CEVAHIR 2019 年 2 月 5 日
I need B and C definition. This answer is not useful for me. Because I am trying to shorten this long script to below script.
for i=1:1000
r = randi([1 3]);
switch r
case 1 %A
x(i+1)=(x(i)+A(1))/2;
y(i+1)=(y(i)+A(2))/2;
case 2 %B
x(i+1)=(x(i)+B(1))/2;
y(i+1)=(y(i)+B(2))/2;
case 3 %C
x(i+1)=(x(i)+C(1))/2;
y(i+1)=(y(i)+C(2))/2;
end
end
for i=1:10000
corners = 'ABC';
randompick = corners(randi(numel(corners)));
disp(randompick)
x(i+1)=(x(i)+randompick(1))/2;
y(i+1)=(y(i)+randompick(2))/2;
end
KSSV
KSSV 2019 年 2 月 5 日
Did you notice that I have copied all the three vertices in a single matrix A...? Why do you think the code is not useful?
A = [A ; B ; C] ;
for i=1:1000
r = randi([1 3]);
x(i+1)=(x(i)+A(r,1))/2;
y(i+1)=(y(i)+A(r,2))/2;
end
FURKAN CEVAHIR
FURKAN CEVAHIR 2019 年 2 月 5 日
My apologize, thanks..
KSSV
KSSV 2019 年 2 月 5 日
No issues.....No aplogizes.......you are welcome.......

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

2019 年 2 月 5 日

コメント済み:

2019 年 2 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by