How to mix integers and chars in matrix?
5 ビュー (過去 30 日間)
古いコメントを表示
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?
採用された回答
Sebastian
2011 年 1 月 27 日
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
0 件のコメント
その他の回答 (3 件)
Sebastian
2011 年 1 月 27 日
Well, you could do something like
>> a = '123ABC';
>> l = size(a,2);
>> rp = randperm(l/2);
>> a1 = a([rp rp+l/2])
a1 =
321CBA
zakri
2011 年 1 月 28 日
9 件のコメント
Paulo Silva
2011 年 1 月 31 日
already did lol
c'mon you just need to put the randperm inside the loop, so easy :)
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!