Cody Problem 33 | Leading answer is in Chinese ?!
1 次查看(过去 30 天)
显示 更早的评论
Salam,
While I was playing with Cody, I found one interesting question that asked to write MATLAB function to create times-table (can be found here: http://bit.ly/1cWZGGM ). The question itself was easy, but I had problems to figure out how could someone solve this problem with compact code of size 10 !!
After I've checked the leading answer, it seems to be written in Chinese not in MATLAB :)
Here is the answer:
function m = timestables(n)
regexp '' '(?@A=repmat([1:n]'',1,n);m=A.*A'';)'
end
Could anyone translate this to me so that I can use it in my future attempts ? :P
Regards.
0 个评论
回答(2 个)
Walter Roberson
2013-12-3
It is a spiritual cheat. For more about how it works see the recent thread http://www.mathworks.co.uk/matlabcentral/answers/106546-how-are-ninja-solutions-even-possible-in-cody
Tom Gaudette
2015-8-19
This is not very readable by all, but I will try to parse this for you.
1) The command actually being executed is regexp(str, pattern), but it is doing this by knowing that MATLAB takes text after a function as a string. So you could rewrite this by saying:
regexp('','(?@A=repmat([1:n]'',1,n);m=A.*A'';)');
2) regexp takes the (?@cmd) with cmd being MATLAB code. You can see this in the doc here. regexp doc
So they are really just running the code:
A=repmat([1:n]',1,n);
m=A.*A';
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!