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);回复:JSL 编程挑战:从 Y 和 N 字符串转换为 1 和 0 矩阵,然后再转换回来如此酷的解决方案。我不敢相信有多种方法可以用来解决这个相对简单的问题。再次感谢大家。
回复:JSL 编程挑战:从 Y 和 N 字符串转换为 1 和 0 矩阵,然后再转换回来这些都是非常酷的解决方案。 PMROZ 指出我误读了 secanrio 是从列表开始。 他要求我提供修改后的解决方案。下面将 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) );回复:JSL 编程挑战:从 Y 和 N 字符串转换为 1 和 0 矩阵,然后再转换回来我在大多数项目中使用通用的“maply”函数,所以通常我只需要最后两行:
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") ) )回复: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);
回复:JSL 编程挑战:从 Y 和 N 字符串转换为 1 和 0 矩阵,然后再转换回来现在我需要研究 design() 函数!
回复:JSL 编程挑战:从 Y 和 N 字符串转换为 1 和 0 矩阵,然后再转换回来哇,这些太棒了。谢谢大家!
回复:JSL 编程挑战:从 Y 和 N 字符串转换为 1 和 0 矩阵,然后再转换回来哎呀,我没有看到克雷格的回复……我认为他的版本更好。
回复:JSL 编程挑战:从 Y 和 N 字符串转换为 1 和 0 矩阵,然后再转换回来您可以使用 Char to Blob 和 Blob to Matrix 在一行中完成字符串到矩阵的转换。 我的贡献:
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)));
回复:JSL 编程挑战:从 Y 和 N 字符串转换为 1 和 0 矩阵,然后再转换回来
如果没有一些额外的努力,这不会扩展到三个值或缺失/错误值。 它转换 string--blob--matrix 并使用矩阵中 ASCII 字符的数量进行 Y 和 N 检测。 BlobToMatrix 和 MatrixToBlob 函数被告知使用 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”;
回复: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);回复:JSL 编程挑战:从 Y 和 N 字符串转换为 1 和 0 矩阵,然后再转换回来无论什么有效,都有效!这是一种替代方案。
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));