JSL プログラミング チャレンジ: Y と N の文字列から 1 と 0 の行列に変換し、またその逆に変換するJSL プログラマーの皆さん、こんにちは。
大規模なアプリケーションの一部として小さな問題が発生したので、それを共有して、より良く/よりスムーズに行う方法があるかどうかを確認したいと思いました。
基本的に、1 と 0 のベクトル (つまり [1, 1, 0, 0, 1]) があり、これを Y と N の文字列 (つまり "YYNNY") に変換する必要があります。次に、その逆、つまり Y と N の文字列を 1 と 0 のベクトルに変換する必要があります。
ここに私の簡単で汚い関数があります - 私の弱い頭脳を超えた何かを思いつくことができるかどうか見てみましょう!
// Convert a vector of 1s and 0s to a string with no separators
matrix_10_to_yn = function({matrix_10}, {default local},
yn_txt = "";
if (nrows(matrix_10) > 0,
// Strip out square brackets and commas
yn_txt = substitute(char(matrix_10), "[", "", "]", "", ", ", "");
// Now convert 1s to Y and 0s to N
yn_txt = substitute(yn_txt, "1", "Y", "0", "N");
);
yn_txt;
);
// Convert a YN string with no separators to a vector of 1s and 0s
yn_to_matrix_10 = function({yn_txt}, {default local},
n = length(yn_txt);
matrix_10 = j(n, 1, 0);
for (i = 1, i <= n, i++,
if (substr(yn_txt, i, 1) == "Y",
matrix_10[i] = 1;
);
);
matrix_10;
);
<BR />// Test thing out
a = yn_to_matrix_10("YYYNNNN"); show(a);
b = matrix_10_to_yn(a); show(b);
c = yn_to_matrix_10(""); show(c);
d = matrix_10_to_yn(c); show(d);Re: JSL プログラミング チャレンジ: Y と N の文字列から 1 と 0 の行列に変換し、またその逆に変換するとてもクールなソリューションです。この比較的単純な問題を解決するためにさまざまなアプローチが使用されているとは信じられません。改めて皆さんに感謝します。
Re: JSL プログラミング チャレンジ: Y と N の文字列から 1 と 0 の行列に変換し、またその逆に変換するこれらはすべて非常に優れたソリューションです。 PMROZ さんは、セカンリオがリストから始まるという私の誤解を指摘しました。 彼は私に修正された解決策を求めました。以下では、YN のリストまたは文字列を 10 のベクトルに変換し、10 のベクトルを YN の文字列に変換します。
前回の投稿と一貫性を保つために、空の文字列区切り文字を使用して、words() 関数を使用して文字列をリストに変換しました。
exmat = J(25,1, RandomInteger(0,1));
exlst = Insert({}, {"Y", "N"}[2-exmat]);
exstr = ConcatItems(Insert({}, {"Y", "N"}[2-exmat]), "");
convert_yn = Function({usr}, {rslt=empty(), ynList ={"Y","N"} },
if(IsMatrix(usr),
rslt = ConcatItems(insert({},ynList[2-usr]),"")
, //else
IsList(usr),
rslt = J(nitems(usr),1, 1);
rslt[loc(usr,"N")]=0
, //else
IsString(usr),
rslt = J(length(Trim(usr)),1,1);
rslt[loc(words(usr,""),"N")]=0
); //end if
rslt
); //end Function
show(exmat, convert_yn(exmat), exlst, convert_yn(exlst), exstr, convert_yn(exstr) );Re: JSL プログラミング チャレンジ: Y と N の文字列から 1 と 0 の行列に変換し、またその逆に変換する私はほとんどのプロジェクトで汎用の 'mapply' 関数を使用するため、通常は最後の 2 行だけが必要です。
Names Default to here(1);
// Utility function to make for loops, combo 'map' and 'apply'
// this is a 'fake' map/apply function because it allows side
// effects (like apply) and it also returns a value (like map)
mapply = function( {x, foo, arg1 = "__missing", arg2 = "__missing"}, { i, r = {}, x, foo, arg1, arg2, arg1missing, arg2missing },
if( N Items( x ) < 1,
return({}), //empty list supplied, return an empty list
for( i = 1, i <= N Items(x), i++,
arg1missing = 0; arg2missing = 0;
If( type(arg1) == "String", If( arg1 == "__missing", arg1missing = 1 ) );
If( type(arg2) == "String", If( arg2 == "__missing", arg2missing = 1 ) );
if(
arg1missing == 1,
r = Insert( r, foo( x[i] ) ),
arg2missing == 1,
r = Insert( r, foo( x[i], arg1 ) ),
r = Insert( r, foo( x[i], arg1, arg2 ) )
)
)
);
return(r);
);
// Solution:
str = "YYYNNNNYYNY";
(numbers = mapply( words(str,""), function( {x}, if( x=="Y", 1, 0) ) ) )
mapply( as list(numbers), function( {x}, if( x==1,"Y", "N") ) )Re: JSL プログラミング チャレンジ: Y と N の文字列から 1 と 0 の行列に変換し、またその逆に変換する@pmroz 、
これについての私の見解/試みは次のとおりです。 この興味深いチャレンジを提供していただきありがとうございます。 ループを使用していることはわかっていますが、それが大規模な行列で適切に拡張できるかどうかはわかりませんが、コミュニティの他の誰かがより高速な方法を考え出すことができるのではないかと思いました。
Clear Log(); Clear Globals();
ex = Associative Array({1,0} ,{"Y", "N"});
Test = [1,0, 1, 1, 1, 0, 0, 1, 0, 1];
Res = {};
for(i = 1, i <= N Rows(Test), i++,
Insert Into(Res, ex << Get Value(Test[i]));
);
Show(Res);
Re: JSL プログラミング チャレンジ: Y と N の文字列から 1 と 0 の行列に変換し、またその逆に変換するそして今度は、design() 関数を勉強する必要があります。
Re: JSL プログラミング チャレンジ: Y と N の文字列から 1 と 0 の行列に変換し、またその逆に変換するうわー、これはすごいですね。皆さんありがとう!
Re: JSL プログラミング チャレンジ: Y と N の文字列から 1 と 0 の行列に変換し、またその逆に変換するおっと、Craig の返信を見ていませんでした...彼のバージョンの方が優れていると思います。
Re: JSL プログラミング チャレンジ: Y と N の文字列から 1 と 0 の行列に変換し、またその逆に変換するChar to Blob および Blob to Matrix を使用して、文字列から行列への変換を 1 行で実行できます。 私の貢献:
names default to here(1);
a=[1,0, 1, 1, 1, 0, 0, 1, 0, 1];
b="YNYYYNNYNY";
f_YNString_to_IndicatorVector=Function({s},
blob to matrix(char to blob(d),"int", 1, "little", 1)==89;
);
f_IndicatorVector_to_YNString=Function({v},{vv},
vv=As List(v);
vv[loc(v)]="Y";
vv[loc(!v)]="N";
concat items(vv,"");
);
show(a);
show(b);
Show(f_YNString_to_IndicatorVector(b));
Show(f_IndicatorVector_to_YNString(a));
Show(f_IndicatorVector_to_YNString(f_YNString_to_IndicatorVector(b)));
Re: JSL プログラミング チャレンジ: Y と N の文字列から 1 と 0 の行列に変換し、またその逆に変換する
特別な努力をしなければ、これは 3 つの値または欠落/エラー値にまで拡張されません。 string--blob--matrix を変換し、行列内の ASCII 文字の数を使用して Y と N の検出を行います。 BlobToMatrix 関数と MatrixToBlob 関数は、1 バイトのリトルエンディアン整数を使用するように指示されます。 エンディアンは 1 バイト数値にとって重要ではなく、大きな値でも同様に機能します。 それは 1 バイト表現を持つ文字に依存します。
source = "YYNNYQ";
// if you are uncomfortable with hard coded constants...
Yvalue = Blob To Matrix( Char To Blob( "Y" ), "int", 1, "little" )[1]; // 89
Nvalue = Blob To Matrix( Char To Blob( "N" ), "int", 1, "little" )[1]; // 78
// forward
numbers = Blob To Matrix( Char To Blob( source ), "int", 1, "little" ) == Yvalue;
// reverse
string = Blob To Char( Matrix To Blob( Nvalue + numbers * (Yvalue - Nvalue), "int", 1, "little" ) );
// display
Show( source, numbers, string );
ソース = "YYNNYQ";
数値 = [1, 1, 0, 0, 1, 0];
文字列 = "YYNNYN";
Re: JSL プログラミング チャレンジ: Y と N の文字列から 1 と 0 の行列に変換し、またその逆に変換する別の!!
Names Default to here(1);
yto1 = function({str},
{DEFAULT LOCAL},
list = words(str, "");
design(list, {"Y"});
);
oneToY = function({mat},
list = substitute(as list(mat), 1, "Y", 0, "N");
Concat items(list, "");
);
str = "YYYNNNNYYNY";
nums = yto1(str);
show(nums);
str2 = oneToY(nums);
show(str2, str2==str);Re: JSL プログラミング チャレンジ: Y と N の文字列から 1 と 0 の行列に変換し、またその逆に変換するうまくいくものは何でもうまくいきます!ここに 1 つの代替案があります。
exmat = J(25,1, RandomInteger(0,1));
exlst = Insert({}, {"Y", "N"}[2-exmat]);
convert_yn = Function({usr}, {rslt, ynList ={"Y","N"} },
if(IsMatrix(usr),
rslt = insert({},ynList[2-usr])
, //else
rslt = J(nitems(usr),1, 1);
rslt[loc(usr,"N")]=0
); //end if
rslt
); //end Function
show(exmat, convert_yn(exmat), exlst, convert_yn(exlst));