2016-07-29 4 views
0

文字列(単語)を整数に、また整数を文字列に割り当てることができるようにしたいので、後で整数または文字列を並べ替えるときにそのユニットに対応する文字列または整数をマトリックスで印刷できます。 例。Matrix - 文字列、整数、および結合演算

103 = QWE 
13 = ASD 
50 = ZXC 
-1 = VBN 
253 = RTY 

など、

105 = QWE 
103 = QWE 

はその後、ちょうど列が他の列をソート.cvs、のよう

matrix = [105,103,13,50,-1,253] 
sort = [-1,13,50,103,105,253] 

print sort_strings 

# output: VBN ASD ZXC QWE QWE RTY 

は、無傷の行を保つために応じて移動します。出力後にこれらの文字列を分類するなど、ファイル上でいくつかの追加機能を実行したいので、視覚化のために色を使っていくつかのチャートを作成することができます。

ありがとう

+3

あなたのタグはPythonとMatlabです。どの言語を使用していますか? –

+3

変数を実際に宣言して何かを割り当てることはできないので、Pythonの辞書がここで使われるかもしれません。 – BusyAnt

+0

Matlabを使用すると、セルのベクトルを作成し、各文字列を対応する位置に割り当てることができます。 A {103} = 'QWE'; A {13} = '13'など...位置の大部分が空の場合は非効率的ですが、 –

答えて

0

これはMATLABで行うことができます。 ベクトル化方法を使ってやろうとしました。そして、それは各ステップで、ますます不明瞭になりましたが、私はそれのために多くの時間を費やしたので、私はそれを示しています。

a1 = ['qve';'rts';'abc';'abc';'def'] 
a2 = [3;5;10;7;8] 
%//find unique strings: 
mycell = unique(a1,'rows') 

%//find numbers corresponded to each string. 
%//You can see there are 2 numbers correspond to string 'abc' 
indexes = arrayfun(@(x) find(all(ismember(a1,mycell(x,:)),2)), 1:size(mycell,1), 'UniformOutput',0) 

%//create some descriptive cell array: 
mycell2 = arrayfun(@(x) a2(indexes{x}), 1:size(mycell,1),'UniformOutput',0) 
mycell = cellstr(mycell) 
mycell = [mycell mycell2'] %' 

%------------------------------------------------------------------------- 
%// now create test array (I use sort like you) 
a3 = sort(cell2mat(mycell(:,2))) 

%//last step: find each index of a3 in every cell of mycell and put corresponding string to res 
res = mycell(arrayfun(@(y) find (cellfun(@(x) any(ismember(x,y)), mycell(:,2))), a3),1) 

a1a2は、入力データです。これは手動で作成されます。 resは、必要な結果です。

res = 

'qve' 
'rts' 
'abc' 
'def' 
'abc' 

P.S.できます!しかし、それはいくつかの頭痛のように見えるので、私はループを使用することをお勧めします。

0

あなたがやっていることは、「並行して配列を並べ替える」または「並列配列を並べ替える」と呼ばれています。これらの用語を使用して、それを行う方法に関するガイドを検索することができます。しかし、以下はMATLABで行う方法の1つを示すコードです:

unsorted_keys = [95, 37, 56, 70, 6, 61, 58, 13, 57, 7, 68, 52, 98, 25, 12]; 

unsorted_strings = cell(size(unsorted_keys)); 

unsorted_strings = {'crumply', 'going', 'coyotes', 'aficionado', 'bob', 'timeless', 'last', 'bloke', 'brilliant', 'reptile', 'reptile', 'reptile', 'reptile', 'reptile', 'reptile'}; 

[sorted_keys, indicies] = sort(unsorted_keys); 

% indicies = [5, 10, 15, 8, 14, 2, 12, 3, 9, 7 6, 11, 4, 1, 13] 

% So, the 5th element of unsorted_keys became the 1st element of sorted_keys 
% the 10th element of unsorted_keys became the 2nd element of sorted_keys 
% the 15th element of unsorted_keys became the 3rd element of sorted_keys 
% the 8th element of unsorted_keys became the 4th element of sorted_keys 
% and so on..... 
% sorted_keys == unsorted_keys(indicies) 

sorted_strings = unsorted_strings(indicies);  
関連する問題