0

為に

A=[100;300;1000;240]

B=cell(8,1)

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

[100]
[300]
[1000]
[240]
[100;300;240]
[100;1000]
[300;1000]
[100;300;1000]

これらを印刷して、出力を次のように表示したい:

choose first
choose second
choose third
choose fourth
choose first or second or fourth
choose first or third
.
.
etc

基本的に、配列から、その中の各値を1つの変数ではなく文字列A=[100;300;1000;240]表す必要があります。これを行う方法はありますか?


ノート :

私のコードでは、ユーザーが配列Aに独自の数値を入力するようにしたいため、 Aの長さは可変であり、4 を超える場合があります。セルBのサイズも数式に従って変化するため、常に固定されているとは限りません。サイズ8で。


また、matlab の専門知識がないため、(必要でない限り) 複雑すぎないシンプルなコードをいただければ幸いです。より単純なコードは、理解と学習に役立ちます。

4

3 に答える 3

1

マッピングにはマップオブジェクトを使用します

index_to_string = containers.Map(keySet,valueSet)

どこ

keySet = 1:20
valueSet = {'first'; 'second'; ...; 'twentieth'}

Aが印刷前に利用できる場合は、同じvalueSetを使用できますA

index_to_string = containers.Map(A,valueSet(1:length(A)))

例:

G = cell(size(B))
for i = 1:length(B)
    out1 = 'choose ';
    if len(B{i}) == 1
        out1 = [out1, index_to_string(B{i})];
    else
        temp = B{i}
        for j=1:(length(temp)-1)
            out1 = [out1, index_to_string(temp(j)), ' or ' ];
        end
        out1 = [out1, index_to_string(temp(end))];
    end
    G{i} = out1
end
于 2013-03-06T23:32:59.740 に答える
0

この回答は完全に回答に基づいていJaredS'sます。私はあなたの疑問を明確にしました。

これをいくつかの m ファイルに書き込みます。

Choices=A; Results=B;
%simple boolean to indicate whether a choice has been made already
answerChosen = 0;

for k = 1:length(Results)
    Response = 'choose';
    for m =  1:length(Choices)
        if any(Results{k} == Choices(m))
            if answerChosen
                Response = [Response ' or ' NumToOrd(m)];
            else
                answerChosen = 1;
                Response = [Response ' ' NumToOrd(m)];
            end
        end
    end
    fprintf('%s\n',Response);
    answerChosen = 0;
 end

次の関数を別のファイルに記述し、以前の m-file と同じディレクトリに配置してください。次に、次のようなエラーが表示されるはずです。"Undefined function 'NumToOrd' for input arguments of type 'double'."

function ordinal = NumToOrd(number)
switch number
    case 1, ordinal = 'first';
    case 2, ordinal = 'second';
    case 3, ordinal = 'third';
    case 4, ordinal = 'fourth';
    otherwise, ordinal = 'out of index';
end
于 2013-03-07T01:19:29.233 に答える
0

これが私がそれを行う方法です

function IChooseYouPikachu(Choices, Results)
% put in A for choices and B for results

%simple boolean to indicate whether a choice has been made already
answerChosen = 0;

for k = 1:length(Results)
    Response = 'choose';
    for m =  1:length(Choices)
        if any(Results{k} == Choices(m))
            if answerChosen
                Response = [Response ' or ' NumToOrd(m)];
            else
                answerChosen = 1;
                Response = [Response ' ' NumToOrd(m)];
            end
        end
    end
    fprintf('%s\n',Response);
    answerChosen = 0;
end

function ordinal = NumToOrd(number)
switch number
    case 1, ordinal = 'first';
    case 2, ordinal = 'second';
    case 3, ordinal = 'third';
    case 4, ordinal = 'fourth';
    otherwise, ordinal = 'out of index';
end
于 2013-03-06T23:22:05.193 に答える