私はここで何が起こっているのか分かります。慎重に文字列を作成してから、eval
、またはeval
、変数名をismember
と呼ぶ必要があります。
いくつかの例は以下のとおりです。
%First some setup
practice_x= [1 2 3];
practice_y = [2 3 4];
practice_z = [1 4 5];
allvariables = who('practice*')
% allvariables =
% 3×1 cell array
% 'practice_x'
% 'practice_y'
% 'practice_z'
%Option 1
for ix = 1:(length(allvariables)-1)
eval(['ismember(' allvariables{ix} ', ' allvariables{ix+1} ')'])
end
%Option 1a (same as 1, but IMHO slightly easier to work with and explain on SO)
for ix = 1:(length(allvariables)-1)
strTemp = ['ismember(' allvariables{ix} ', ' allvariables{ix+1} ')'];
%When ix = 1, the strTemp variable contains the string below.
% strTemp =
% ismember(practice_x, practice_y)
eval(strTemp)
end
%Option 2, use `eval` on the variable names directly
for ix = 1:(length(allvariables)-1)
ismember( eval(allvariables{ix}), eval(allvariables{ix+1}) )
end
%For this example, all of these options result in the following output
% ans =
% 1×3 logical array
% 0 1 1
% ans =
% 1×3 logical array
% 0 0 1
標準知識をひけらかす訓戒:通常ことを意味し、データとして変数名のこのタイプの操作を強制的に、変数名で情報を格納伴う
質問全体的なコードは、ストレスに満ちていて、扱いにくい方法で構成されています。
これは機能します。これは、Matlabの文書化された機能と一貫しています。しかし、このコードのどこかには、データがどのように処理され、保存されているかに関する強い反パターンがあります。
私はここで 'eval'コマンドが必要ないとは思いません。あなたはそれを単に取り除くことができますか? – Pursuit
いいえ、私は各配列の内容を比較する必要がありますし、allvariables = who( 'practice *')にはコンテンツがありません。 – Paula
私は@Pursuitに同意します。それは 'eval'の仕組みではなく、ここでは不要です。 – Adriaan