0

私は次の行列配列Bを持っています:

B=[1 2 3; 10 20 30 ; 100 200 300 ; 500 600 800];

コードを介してどちらを組み合わせて、値間の可能な組み合わせを形成します。結果はセルGに保存されます。G:

G=
[1;20;100;500]
[0;30;0;800]
[3;0;0;600]
.
.
etc

選択した値に基づいて結果をフォーマットしたいB

[1 2 3]      = 'a=1 a=2 a=3'
[10 20 30]   = 'b=1 b=2 b=3'
[100 200 300]= 'c=1 c=2 c=3'
[500 600 800]= 'd=1 d=2 d=3'

例として、提供されている現在のセルの結果を使用します。

[1;20;100;500]
[0;30;0;800]
[3;0;0;600]

次のように印刷する必要があります

a=1 & b=2 & c=1 & d=1 
a=0 & b=3 & c=0 & d=3  % notice that 0 should be printed although not present in B
a=3 & b=0 & c=0 & d=2

セルGはコードによって異なり、固定されていないことに注意してください。結果の生成に使用されたコードはここで表示できます:Matlabでこのコードをデバッグするためのヘルプが必要です


これについてさらに情報が必要な場合はお知らせください。

4

1 に答える 1

1

あなたはこれを試すことができます:

k = 1; % which row of G

string = sprintf('a = %d, b = %d, c = %d, d = %d',...
    max([0 find(B(1,:) == G{k}(1))]),  ...
    max([0 find(B(2,:) == G{k}(2))]),  ...
    max([0 find(B(3,:) == G{k}(3))]),  ...
    max([0 find(B(4,:) == G{k}(4))])  ...
    );

たとえばk = 1、サンプルデータの場合、これは次のようになります。

string =

a = 1, b = 2, c = 1, d = 1

このコードの簡単な説明(コメントで要求されている)は次のとおりです。簡単にするために、例はGの最初の値とBの最初の行に限定されています。

% searches whether the first element in G can be found in the first row of B
% if yes, the index is returned
idx = find(B(1,:) == G{k}(1));

% if the element was not found, the function find returns an empty element. To print  
% a 0 in these cases, we perform max() as a "bogus" operation on the results of  
% find() and 0. If idx is empty, it returns 0, if idx is not empty, it returns 
% the results of find().
val = max([0 idx])

% this value val is now formatted to a string using sprintf
string = sprintf('a = %d', val);
于 2013-03-08T07:49:09.883 に答える