0

にとって

A=[1;3;5]

B=cell(7,1)

次の結果がセルに保存されています

[1]
[3]
[5]
[1;3]
[1;5]
[3;5]
[1;3;5]

、、、のようにprint結果を出したい。-基本的に、Aの各値を変数に割り当てます。a=1b=3c=5

Matlabでこれを行うにはどうすればよいですか?


私はこのようなものになる可能性のある結果を探しています:

" you can have a "
" you can have b "
" you can have c "
" you can have a or b "
.
.
etc
4

2 に答える 2

1

私が正しく理解している場合、次のようなものが必要です。

numToLetter = [ 'a', ' ', 'b', ' ', 'c' ];
B = { 1, 3, 5, [ 1; 3 ], [ 1; 5 ], [ 3; 5 ], [ 1; 3; 5 ] };

% Loop though each entry in our cell array
for i = 1 : length(B)

  fprintf(' you can have ');               % Print initial message

  % Loop though each vector element inside the B{i}
  for j = 1 : length(B{i})
    fprintf('%c', numToLetter(B{i}(j) ) )  % Use our numToLetter lookup table
                                           % to convert the number to a letter,
                                           % and print it out.

    if j ~= length(B{i})
      fprintf(' or ');                     % Print 'or' if there are more to come
    end
  end
  fprintf('\n');                           % New line
end

あなたの質問の主な部分は、各数字を文字に割り当てる方法でした(注:各数字を変数に割り当てるように頼んだことは知っていますが、それはあなたが望むものではないと思います.)。numToLetterこれは、と呼ばれるルックアップ テーブルを使用して行われます。このテーブルは、、 、および に格納aされています。このようにして、入力数値をこのテーブルへのインデックスとして使用するだけです。このルックアップ テーブルをベクトルで使用できます。例えば:1b3c5

myNumbers = [ 1 3 3 1 5 ];
myLetters = numToLetter(myNumbers)

次の出力が得られます。

myLetters =

abbac
于 2013-03-06T09:14:20.540 に答える
1

の数字Cに割り当てる文字の配列を としますA。それで

A = [1 3 5];
B = {[1]; [3]; [5]; [1;3]; [1;5]; [3;5]; [1;3;5]};
C = ['a', 'b', 'c']

k = 6; % indicates current line of B
str = ['you can have ' strrep(strrep(sprintf('_%c_', ...
    C(ismember(A, B{k}))'), '__', ' or '), '_', '')];

結果は

str =

you can have a or b or c

一度にすべてのフィールドへの応答を作成する 場合は、次を使用できますB

allStr = arrayfun(@(x) ['you can have ' strrep(strrep(sprintf('_%c_', ...
    C(ismember(A, B{x}))'), '__', ' or '), '_', '')], ...
    (1:length(B))', 'uniformoutput', false)

これにより、

allStr = 

    'you can have a'
    'you can have b'
    'you can have c'
    'you can have a or b'
    'you can have a or c'
    'you can have b or c'
    'you can have a or b or c'

このコードの段階的な説明は次のとおりです。

% which contents of A can be found in B?
idx = ismember(A, B{k})'; 

% to which letters do these indices correspond?
letters = C(idx);

% group the letters in a string embedded in '_' as place holders for later use
% by this, the places between letters will be marked with '__' and the places 
% at the beginning and the end of the string will be marked with '_'
stringRaw = sprintf('_%c_', letters); 

% replace each occurrence of '__' by ' or '
stringOr = strrep(stringRaw, '__', ' or ');

% replace each occurrence of '_' by ''
stringClean = strrep(stringOr, '_', ''); 

% add first half of sentence
stringComplete = ['you can have ' stringClean];

(コメントで要求されているように)完全な単語でこれを機能さCせるには、文字列のセル配列に変換し、それに応じて式を更新する必要があります。

A = [1 3 5];
B = {[1]; [3]; [5]; [1;3]; [1;5]; [3;5]; [1;3;5]};
C = {'first', 'second', 'third'}

k = 7; % indicates current line of B
str = ['you can have ' strrep(strrep(sprintf('_%s_', ...
    C{ismember(A, B{k})}), '__', ' or '), '_', '')];

これにより、次の結果が得られます。

str =

you can have first or second or third
于 2013-03-06T09:54:03.417 に答える