How to mix integers and chars in matrix?

1. I want to create the following matrix:
A=[1 2 3 A B C]
2. After that, based on the matrix A, I want to generate random matrix of this following matrix:
A1=[1 3 2 A C B]
A2=[2 1 3 B A C]
A2=[2 3 1 B C A]
Does any body can help me?

1 件のコメント

Paulo Silva
Paulo Silva 2011 年 1 月 27 日
Not possible inside matrix, you must use cells

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

 採用された回答

Sebastian
Sebastian 2011 年 1 月 27 日

1 投票

As Paulo pointed out, this most likely requires usage of cells, e.g.:
>> A = {1 2 3 'A' 'B' 'C'};
>> A1 = A(randperm(6))
A1 =
[3] 'A' [1] 'C' [2] 'B'
>> A2 = A(randperm(6))
A2 =
[3] [2] 'C' 'A' 'B' [1]
However, if the integers and chars are always 1 digit/characte, then you can maybe also use the following approach:
>> a = '123ABC';
>> a1 = a(randperm(6))
a1 =
13A2CB
>> a2 = a(randperm(6))
a2 =
31BAC2

その他の回答 (3 件)

zakri
zakri 2011 年 1 月 27 日

0 投票

Thank you very much. I really appreciate it. However, how do we want to make the alphabet change according to the integer? for instance:
Matrix A is :
A=[1 2 3 A B C]
where
A (column 3) is dependant to 1 (column 0)
B(column 4) is dependant to 2 (column1)
C(column 5) is dependant to 3 (column 2)
if the integer 1, 2 and 3 is randomly permute within the columns
0 to 2, the alphabets of A,B and C are also randomly permute within columns 3 to 5.
Sebastian
Sebastian 2011 年 1 月 27 日

0 投票

Well, you could do something like
>> a = '123ABC';
>> l = size(a,2);
>> rp = randperm(l/2);
>> a1 = a([rp rp+l/2])
a1 =
321CBA

1 件のコメント

zakri
zakri 2011 年 1 月 28 日
thank you. It helps me a lot.

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

zakri
zakri 2011 年 1 月 28 日

0 投票

hi again... How to repeatedly do random such: 321CBA 312CAB 213BAC
I type a code as the following:
A='123ABC'
AA=[];
randormsort=[];
l=size(a,2);
rp=randperm(l/2);
for i=1:3
AA(i,:)=A([rp rp+l/2]);
end
randomsort=[AA];
however the result appear as follows:
AA =
49 51 50 65 67 66
49 51 50 65 67 66
49 51 50 65 67 66
the result that i am expected should be something like this:
3 2 1 C B A
2 1 3 B A C
2 3 1 B C A

9 件のコメント

Paulo Silva
Paulo Silva 2011 年 1 月 28 日
char(AA)
zakri
zakri 2011 年 1 月 28 日
it returns only
321CBA
Paulo Silva
Paulo Silva 2011 年 1 月 28 日
find what's wrong with your code, that's the better way to learn.
Paulo Silva
Paulo Silva 2011 年 1 月 28 日
hint: You got all the code in there but one line of it is in the wrong place :)
zakri
zakri 2011 年 1 月 29 日
I have change to :
A='123ABC'
AA=[];
randormsort=[];
l=size(A,2);
rp=randperm(l/2);
for i=1:3
AA(i,:)=A([rp rp+l/2]);
end
randomsort=char(AA);
it works. however, the rows in the same value:
213BAC
213BAC
213BAC.
how to make it be in such a way that rows 2 and 3 are different sequence from rows 1?
Paulo Silva
Paulo Silva 2011 年 1 月 29 日
You are only shuffling the cards once, that's why you get always the same result, shuffle the cards 3 times...
zakri
zakri 2011 年 1 月 31 日
can you give a hint?
Paulo Silva
Paulo Silva 2011 年 1 月 31 日
already did lol
c'mon you just need to put the randperm inside the loop, so easy :)
zakri
zakri 2011 年 1 月 31 日
It works!!thank you.

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

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

質問済み:

2011 年 1 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by